javascript 如何访问实例上的静态成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19470559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:42:00  来源:igfitidea点击:

How to access static member on instance?

javascriptoopstatic-methodsstatic-members

提问by Alan Kis

Here is code, I've been struggling for hours with that, idea is to keep track of how many instances is created, but also make possible to call static method and change/update static member. There is similar question, but I can't implement any solution to my problem.

这是代码,我已经为此苦苦挣扎了几个小时,想法是跟踪创建了多少实例,但也可以调用静态方法和更改/更新静态成员。有类似的问题,但我无法对我的问题实施任何解决方案。

  // object constructor
function Foo() {
    this.publicProperty = "This is public property";
}
// static property
Foo.staticProperty = "This is static property";

// static method
Foo.returnFooStaticProperty = function() {
    return Foo.staticProperty;
};


console.log("Static member on class: " + Foo.staticProperty); // This is static property


console.log("Result of static method: " + Foo.returnFooStaticProperty()); //This is static property

var myFoo = new Foo();
console.log("myFoo static property is: " + myFoo.staticProperty); // undefined

For sake of simplicity I have changed constructor and member names here. I think it is obvious what happening. I want both of constructor object and instance share same static property.

为简单起见,我在这里更改了构造函数和成员名称。我认为很明显发生了什么。我希望构造函数对象和实例共享相同的静态属性。

I can access static member on constructor object, but on instance I got undefined.

我可以访问构造函数对象上的静态成员,但在实例中我没有定义。

采纳答案by Xitalogy

EDIT: I re-read your question, and this code solves your actual problem as stated:

编辑:我重新阅读了您的问题,此代码解决了您的实际问题,如下所述:

JavaScript:

JavaScript:

function Foo() {
    this.publicProperty = "This is public property";
    Object.getPrototypeOf(this).count++;
}
Foo.prototype.count = 0;

console.log(new Foo().count, new Foo().count, Foo.prototype.count);

FIDDLE

小提琴

This does not decrement the count automatically though if the object is freed up for garbage collection, but you could use deletethen manually decrement.

如果对象被释放用于垃圾收集,这不会自动减少计数,但您可以使用删除然后手动减少。

回答by romik

You can try to get access to static property via constructor

您可以尝试通过构造函数访问静态属性

console.log(myFoo.constructor.staticProperty);

回答by mor

Javascript is prototype based. In other words, all instances of an object share the same prototype. You can then use this prototype to put static shared properties:

Javascript 是基于原型的。换句话说,一个对象的所有实例共享同一个原型。然后你可以使用这个原型来放置静态共享属性:

function Foo() {
  this.publicProperty = "This is public property";
}

Foo.prototype.staticProperty = "This is static property";

var myFoo = new Foo();
console.log(myFoo.staticProperty); // 'This is static property'

Foo.prototype.staticProperty = "This has changed now";
console.log(myFoo.staticProperty); // 'This has changed now'