Javascript 在OLOO继承中不共享对象属性

Javascript 在OLOO继承中不共享对象属性,javascript,Javascript,我不确定在一个OLOO继承链中每个对象都有单独的对象属性的最佳方法 检查这个小提琴或考虑下面的代码: 在小提琴中,单击foo或bar文本将调用foo.add(…)或bar.add(…)函数向对象数组添加元素,从而在输出中产生一个额外的标记。 结果不是我想要的。foo和bar共享同一个数组。但是很容易理解发生了什么,如果我们查看对象继承,我们可以看到以下内容: 好吧,那么,我能做些什么来解决这个问题呢?我想到了两个选择: 选项1) 这将创建一个新的父对象,在每次创建父对象或子对象从(新)父对

我不确定在一个OLOO继承链中每个对象都有单独的对象属性的最佳方法

检查这个小提琴或考虑下面的代码:

在小提琴中,单击foo或bar文本将调用foo.add(…)或bar.add(…)函数向对象数组添加元素,从而在输出中产生一个额外的
标记。
结果不是我想要的。foo和bar共享同一个数组。但是很容易理解发生了什么,如果我们查看对象继承,我们可以看到以下内容:

好吧,那么,我能做些什么来解决这个问题呢?我想到了两个选择:

选项1)

这将创建一个新的父对象,在每次创建父对象或子对象从(新)父对象“继承”时创建父对象的所有函数。虽然这解决了我遇到的问题,但总是为每个子类型对象反复创建相同的函数似乎不是个好主意

选项2)

这似乎也解决了问题,但避免了重新创建父对象及其函数。那么这就是路吗?如果继承链中有多个子项也有私有属性呢

像这样的

Child = Object.create(Parent, {
    ctor: { value: function(someArgs) {
        this.__proto__.ctor(someArgs);
        this.otherPrivate = {};
        // maybe use someArgs
        return this;
    }},

    removeAllButOne: { value: function() {
        this.array.splice(1);
        return this;
    }}
});
孩子们将用他们自己的函数跟踪父ctor。。。但是在他们的ctor函数中,他们可以调用父ctor来不破坏功能

非常感谢您的想法和建议,谢谢

最简单的方法是使用构造函数,以便始终在实例上将数组创建为自己的属性

// define Parent
function Parent() {
    this.array = []; // array will be an instance property
}
Parent.prototype = {}; // inherit all the goodies from Object.prototype
Object.assign(Parent.prototype, { // using `Object.assign` for shorthand
    add: function (element) {
        this.array.push(element + this.array.length.toString());
        return this;
    },
    getAll: function () {
        return this.array;
    }
});

// define Child
function Child() {
    Parent.apply(this); // apply Parent constructor to the instance
}
Child.prototype = Object.create(Parent.prototype); // inherit Parent's prototype chain
Object.assign(Child.prototype, {
    removeAllButOne: function () {
        this.array.splice(1);
        return this;
    }
});
现在有

var a = new Child(),
    b = new Child();
a.array === b.array; // false

您也可以使用ES 6的类来编写它,但这只是我上面所写内容的语法糖,将产生相同的结构。

更喜欢组合而不是继承。您可以将factory方法模式与
对象一起使用。分配
以通过简单的原型委托组合对象:

//可组合原型对象,或“特征”
var基={
添加:功能(元素){
this.array.push(元素+this.array.length.toString());
归还这个;
},
getAll:function(){
返回此.array;
}
};
变量canRemoveAllButOne={
removeAllButOne:函数(){
这个.阵列.拼接(1);
归还这个;
}
}
//工厂职能
//你可以把它们想象成外部构造函数
函数createBase(){
返回Object.assign({},base{
数组:[]
})
}
函数createComposited(){
var base=createBase();
return Object.assign(base,canRemoveAllButOne)
}
//试验
功能日志{
文件。写(s+“
”); } var b1=createBase(); var b2=createBase(); var c1=createcomposited(); var c2=createcomposited(); b1.加入(1); b1.加入(2); b2.加入(9); c1.添加(‘a’); c2.添加(‘b’); log(b1.getAll()); log(b2.getAll()); log(c1.getAll());
log(c2.getAll())的可能重复项。是的,使用构造函数。如果你愿意使用库,可能会有所帮助。我认为你不是真的想要
Parent.prototype=Object.create(null)。从
对象
继承可能吗?我认为
.hasOwnProperty()
.isPrototypeOf()
对象继承。原型
在纯继承中非常有价值(同样,
对象的实例
)。但当然,你是否想要
对象
取决于你自己。我敢打赌,当你到处使用非
对象
-对象时,会有很多库崩溃:-)@PaulS:是的,我个人的偏好是使用
hasOwnProperty。也可以调用
,但我很少在其他地方看到它。顺便说一句,我不反对这种模式;我试图用支持/反对OLOO的论点来概括。@joews
a
继承了一个对象
C
(我们称之为
Child.prototype
),
C
继承了一个对象
P
(我们称之为
Parent.prototype
P
has属性getAllDisclaimer:我从未在实际代码中尝试过这种模式,我并不特别喜欢
base
可以移除所有butone
假设存在
this.array
,而不自己创建它的方式。我认为OLOO喜欢行为委托,同时仍然维护和使用原型链接。但是在你的代码中,你没有以通常的方式使用原型链接。编辑:打那个,我想我现在明白了。不,你是对的。我不知何故将OLOO与“串联继承”(与OLOO并驾齐驱)混为一谈。真正的OLOO方法看起来像
Object.assign(Object.create(parent),childAttributes)
。稍后我会更新答案以澄清这一点。
Child = Object.create(Parent, {
    ctor: { value: function(someArgs) {
        this.__proto__.ctor(someArgs);
        this.otherPrivate = {};
        // maybe use someArgs
        return this;
    }},

    removeAllButOne: { value: function() {
        this.array.splice(1);
        return this;
    }}
});
// define Parent
function Parent() {
    this.array = []; // array will be an instance property
}
Parent.prototype = {}; // inherit all the goodies from Object.prototype
Object.assign(Parent.prototype, { // using `Object.assign` for shorthand
    add: function (element) {
        this.array.push(element + this.array.length.toString());
        return this;
    },
    getAll: function () {
        return this.array;
    }
});

// define Child
function Child() {
    Parent.apply(this); // apply Parent constructor to the instance
}
Child.prototype = Object.create(Parent.prototype); // inherit Parent's prototype chain
Object.assign(Child.prototype, {
    removeAllButOne: function () {
        this.array.splice(1);
        return this;
    }
});
var a = new Child(),
    b = new Child();
a.array === b.array; // false