简体   繁体   English

在 Javascript 中跨对象组共享属性

[英]sharing attributes across groups of objects in Javascript

In javascript if I need objects each to have an individual value of an attribute, I can just set it in the constructor, like so:在 javascript 中,如果我需要每个对象都有一个单独的属性值,我可以在构造函数中设置它,如下所示:

function A(x) {
    this.a = x;
}

If I want a certain attribute to be shared among the objects, I can set it in the prototype of constructor function.如果我想让某个属性在对象之间共享,我可以在构造函数function的原型中设置它。

function B() {}
B.prototype.b = 'B0';

But what to do in in-between situation?但是在中间情况下该怎么办? Basically, I have an existing code where all the constructed objects inherit a property from a prototype, but now I need to divide them into several groups so all members of a group share an attribute.基本上,我有一个现有代码,其中所有构造的对象都从原型继承一个属性,但现在我需要将它们分成几个组,以便一个组的所有成员共享一个属性。

Is there a way to specialize the constructor function B somehow?有没有办法以某种方式专门化构造函数 function B?

B.prototype.b does NOT create a static property as you presume. B.prototype.b不会像您假设的那样创建 static 属性。 It's a bit more complicated than that, properties attached to a prototype share their value with other instances, until they overwrite that value, meaning that:比这复杂一点,附加到原型的属性与其他实例共享它们的值,直到它们覆盖该值,这意味着:

var Foo = function(){
};
Foo.prototype.bar = 'bar';

var f1 = new Foo();
console.log( f1.bar ); //outputs 'bar'
var f2 = new Foo();
console.log( f2.bar ); //outputs 'bar'

f2.bar = 'notbar';
console.log( f2.bar ); //outputs 'notbar'
console.log( f1.bar ); //outputs 'bar'

The only way to have "real" static properties is to attach them to the constructor function itself:拥有“真正的” static 属性的唯一方法是将它们附加到构造函数 function 本身:

Foo.bar2 = 'bar2';

instances of Foo will have to access that value with Foo.bar2 . Foo的实例必须使用Foo.bar2访问该值。

So the answer to your question is to create "subclasses" (constructor functions that inherit their prototype from a base constructor function) for each group and attach a property per subclass, like this:所以你的问题的答案是为每个组创建“子类”(从基本构造函数继承其原型的构造函数)并为每个子类附加一个属性,如下所示:

var Base = function(){
};
Base.prototype.getSomething = function(){
    return this.constructor.something;
};
Base.prototype.setSomething = function( value ){
    this.constructor.something = value;
}
var Group1 = function(){
};
Group1.prototype = new Base(); //classical inheritance
Group1.prototype.constructor = Group1;
Group1.something = 'something';
var Group2 = function(){
};
Group2.prototype = new Base(); //classical inheritance
Group2.prototype.constructor = Group2;
Group2.something = 'something else';

var g1a = new Group1();
var g1b = new Group1();
var g2a = new Group2();
var g2b = new Group2();

g1a.setSomething( 'whatever' );

console.log( g1a.getSomething() ); //outputs 'whatever'
console.log( g1b.getSomething() ); //outputs 'whatever'
console.log( g2a.getSomething() ); //outputs 'something else'
console.log( g2b.getSomething() ); //outputs 'something else'

Warning: Group1.prototype = new Base();警告: Group1.prototype = new Base(); is actually bad practice, I wrote a blog post about 3 types of inheritance just a few days ago which explains why:实际上是不好的做法,几天前我写了一篇关于 inheritance 的 3 种类型的博文,其中解释了原因:

http://creynders.wordpress.com/2012/04/01/demiurge-3-types-of-javascript-inheritance-2/ http://creynders.wordpress.com/2012/04/01/demiurge-3-types-of-javascript-inheritance-2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM