简体   繁体   English

Javascript:数组成员变量在原型继承中不作为实例变量工作

[英]Javascript: array member variable not working as instance variable in prototypical inheritance

I am trying to use javascript inheritance in my code. 我试图在我的代码中使用javascript继承。 Following is the jsfiddle for the same. 以下是相同的jsfiddle。 http://jsfiddle.net/Fetsz/3/ http://jsfiddle.net/Fetsz/3/

var baseObject = Object.create(Object, {  
    // this works as an instance variable for derived objects  
    name: {
        writable: true,
        configurable: true,
        enumerable: true,
        value: null    
    },   
    // this should act as instance variable for derived objects   
    values: {
        writable: true,
        configurable: true,
        enumerable: true,
        value: []    
    },
    displayAlert: {
        value: function (){
            alert("Alert from base object");
        }
    }
});

var derivedObj1 = Object.create(baseObject, {});    
var derivedObj2 = Object.create(baseObject, {});    

function displayValues (obj) {
    alert("values for " + obj.name + "\n" + JSON.stringify(obj.values));
};

$(document).ready(function(){
    derivedObj1.name = "Object 1";
    derivedObj2.name = "Object 2";

    derivedObj1.values.push("DO1 element");
    derivedObj2.values.push("DO2 element");

    derivedObj1.displayAlert();

    // should display values added only for derivedObj1
    displayValues(derivedObj1);

    // should display values added only for derivedObj2
    displayValues(derivedObj2);
});

I also checked following question which nicely explains why my code doesn't work. 我还检查了以下问题,很好地解释了为什么我的代码不起作用。 Javascript Inheritance: Parent's array variable retains value Javascript继承:Parent的数组变量保留值

I need to have an array member variable in every derived class which will contain certain values. 我需要在每个派生类中都有一个数组成员变量,它将包含某些值。 I was trying to achieve the same by adding array in base class. 我试图通过在基类中添加数组来实现相同的目标。 As mentioned in above question, to achieve the same, I will have to add array member to each derived class. 正如上面提到的问题,为了实现相同的目的,我将不得不为每个派生类添加数组成员。 Is there any other way to achieve this without modifying every derived class? 有没有其他方法来实现这一点而不修改每个派生类?

When they are created, both derivedObj1.values and derivedObj2.values point to the same array object. 创建它们时, derivedObj1.valuesderivedObj2.values指向同一个数组对象。 If you were to first reassign each one using derivedObj1.values = []; 如果你首先使用derivedObj1.values = [];重新分配每一个derivedObj1.values = []; , in the same way as you are reassigning the name property, your code would behave as expected. ,就像重新分配name属性一样,您的代码将按预期运行。

Instead of adding the values property to the original baseObject, you could create an init method that adds an array to the instance: 您可以创建一个将数组添加到实例的init方法,而不是将values属性添加到原始baseObject:

var baseObject = Object.create(Object, {  
    // this works as an instance variable for derived objects  
    name: {
        writable: true,
        configurable: true,
        enumerable: true,
        value: null    
    },   
    // this should act as instance variable for derived objects   
    init: {
        writable: true,
        configurable: true,
        enumerable: true,
        value: function(){
            this.values = [];
        }
    },
    displayAlert: {
        value: function (){
            alert("Alert from base object");
        }
    }
});

var derivedObj1 = Object.create(baseObject, {});
derivedObj1.init();
var derivedObj2 = Object.create(baseObject, {});
derivedObj2.init();

Your variables can't be on the prototype if you don't want them to be shared by all instances. 如果您不希望所有实例共享变量,则您的变量不能在原型上。 You could create a second object with just the property definition for the array,and pass it as the second parameter to Object.create : 您可以仅使用数组的属性定义创建第二个对象,并将其作为第二个参数传递给Object.create

var arrDef = {
        values: {
            writable: true,
            configurable: true,
            enumerable: true,
            value: []
        }
}
var derivedObj1 = Object.create(baseObject, arrDef);    
var derivedObj2 = Object.create(baseObject, arrDef);    

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

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