我可以在创建对象时保留对象的副本 - 续:

时间:2012-12-20 09:38:10

标签: javascript object

这是 My Old Question

的延续

这是我创建新学生对象的函数:

function student(id, name, marks, mob, home){
    this.id = id;
    this.name = name;
    this.marks = marks;
    this.contacts = {};
    this.contacts.mob = mob;
    this.contacts.home = home;

    this.toContactDetailsString = function(){
        return this.name +':'+ this.mob +', '+ this.home
    }
}

我想在该对象内初始化对象时创建该对象的副本: 我想出了这个:

function student(id, name, marks, mob, home){
    this.id = id;
    this.name = name;
    this.marks = marks;
    this.contacts = {};
    this.contacts.mob = mob;
    this.contacts.home = home;

    this.toContactDetailsString = function(){
        return this.name +':'+ this.mob +', '+ this.home
    }
    this.baseCopy = this; //Not sure about this part
}

但问题是它给我一个无限循环的baseCopy当前对象的副本;当我更新我的对象的任何属性时,它也会自动更新。

1。这怎么可能,我可以在创建它时在该对象内部保留一个具有初始值的对象的副本?

2。是否可以不复制功能

第3。我很好奇,如果没有硬编码属性名称和使用纯JS

,这是否可行

4 个答案:

答案 0 :(得分:3)

您不是通过说

来创建副本

this.baseCopy = this;,您只是设置对此内部变量的引用。所以baseCopy也指向同一个对象

您需要创建一个方法,该方法将从传递的学生对象返回一个新的学生对象,然后将其存储为BaseCopy

答案 1 :(得分:3)

就像我之前的问题的回答一样,您可以使用此代码制作对象的副本及其嵌套属性,而不是复制它的功能:

function student(id, name, marks, mob, home){
    this.id = id;
    this.name = name;
    this.marks = marks;
    this.contacts = {};
    this.contacts.mob = mob;
    this.contacts.home = home;

    this.toContactDetailsString = function(){
        return this.name +':'+ this.mob +', '+ this.home
    }

    // Copy the object to baseCopy 
    this.baseCopy = clone(this); // "clone" `this.baseCopy`
}

function clone(obj){
    if(obj == null || typeof(obj) != 'object'){ // If the current parameter is not a object (So has no properties), return it;
        return obj;
    }

    var temp = {};
    for(var key in obj){ // Loop through all properties on a object
        if(obj.hasOwnProperty(key) && !(obj[key]  instanceof Function)){ // Object.prototype fallback. Also, don't copy the property if it's a function.
            temp[key] = clone(obj[key]);
        }
    }
    return temp;
}

var s = new student(1, 'Jack', [5,7], 1234567890, 0987654321);
s.marks = s.marks.concat([6,8]); // Jack's gotten 2 new marks.

console.log(s.name + "'s marks were: ", s.baseCopy.marks);
// Jack's marks were:  [5, 7]
console.log(s.name + "'s marks are: ", s.marks);
// Jack's marks are:  [5, 7, 6, 8]
console.log(s.baseCopy.toContactDetailsString); // check if the function was copied.
// undefined
console.log(s.baseCopy.contacts.mob);
// 1234567890

(我会在深度复制上工作一段时间)

“深层”副本现在可以正常使用。

答案 2 :(得分:1)

this.baseCopy = new student(id, name, marks);

你的方式只是做一个循环引用。使用new实例化一个新对象。

使用它可能会进入无限递归。

你可以这样绕过这个:

function student(id, name, marks, flag) {
    // usual code...
    // And:
    if (!flag) {
        this.baseCopy = new student(id, name, marks, true);
    }
}

这样,只有顶尖学生才有baseCopy。

答案 3 :(得分:1)

嗯...

this.baseCopy = this;

基本上意味着对象的baseCopy是对象本身。所以:

var abc = new student(someId, someName, someMarks);

abc和abc.baseCopy实际上指向同一个对象。 您可以做的是将baseCopy更改为:

this.baseCopy = { id: id, name: name, marks: marks, contact: {mob:mob, home: home}}

基本上手动创建对象的输入副本。 请注意,如果任何输入是引用类型,则副本仍将指向与原始对象相同的对象。