简体   繁体   English

javascript对象属性具有指向另一个属性的指针

[英]javascript object property has pointer to another property

I have this code, wich adds Node to array and then changes it, the firstNode object is set in the init function. 我有这段代码,将Node添加到数组,然后对其进行更改,在init函数中设置了firstNode对象。

var myClass = function() {
    this.items = []
    this.firstNodeValues = {};

    this.init = function() {
        var firstIndex = false;

        for (i = 10; i <= 15; i++) {
            Node = {};
            Node.index = i;
            Node.name = "undefined";
            if (i == 10) {
                Node.name = "John"
                this.firstNodeValues = Node;
            }
            this.items[i] = Node;
        }
    }

    this.iterate = function() {
        var newIndex = 10;
        for (i = 10; i <= 15; i++) {
            this.items[i].index = newIndex;
            if (i == 10) {
                this.items[i].name = "Michael";
            }
            newIndex++;
        }
    }
}
var test = new myClass();
test.init();
console.debug(test.firstNodeValues.name) // this gives value "John"
test.iterate();
console.debug(test.firstNodeValues.name) // this gives value "Michael"

The second debug returns "Michael" but why? 第二次调试返回"Michael"但是为什么呢? It looks the firstNodeValues has a pointer to the node items[10] . 看起来firstNodeValues有一个指向节点items[10]的指针。
I want that if I set the FirstNodeValues in firstTime and later a change Node values then the firstNodeValues is not Changes and stay exactly the same as I first set it. 我想,如果我设置FirstNodeValuesfirstTime后来改变节点值则firstNodeValues没有变化,并保持完全一样,我第一次设置它。

The reason your clone is changing when you change your data is this: 更改数据时克隆更改的原因是:

this.firstNodeValues = Node;

There, you are declaring firstNodeValues to be a reference to Node , not a copy . 在这里,您声明firstNodeValues是对Node引用 ,而不是copy So everything you change in firstNodeValues , changes in Node , and vice versa. 因此,您在firstNodeValues中更改的所有内容,在Node更改的firstNodeValues ,反之亦然。

You'll need to clone the object, like this: 您需要克隆对象,如下所示:

function clone(obj){
    if(obj == null || typeof(obj) != 'object'){
        return obj;
    }

    var temp = {};

    for(var key in obj){
        if(obj.hasOwnProperty(key))){
            temp[key] = clone(obj[key]);
        }
    }
    return temp;
}

Then, you can copy the node, like this: 然后,您可以复制节点,如下所示:

this.firstNodeValues = clone(Node);

This will make sure that this.firstNodeValues isn't a reference to Node , but a new object, with the same properties / values as Node (So, a clone). 这将确保this.firstNodeValues不是对Node的引用,而是一个具有与Node相同的属性/值的新对象(因此,是一个克隆)。

You might be interested in jQuery.extend() . 您可能对jQuery.extend()感兴趣。 So: 所以:

this.firstNodeValues = $.extend( {}, Node );

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

相关问题 Javascript-对象的属性在另一个属性中? - Javascript - property of an object in another property? 根据javascript中的另一个属性求和对象属性 - Sum Object property accoring to another property in javascript JavaScript:在另一个属性中使用对象的属性吗? - JavaScript: Using an object's property in another property? 关于指向对象属性的Javascript指针的问题 - Question about Javascript pointer to an object property 将JavaScript对象属性与同一JavaScript对象中的另一个属性进行映射 - Mapping JavaScript Object property with another property in same JavaScript Object JavaScript:如果另一个对象在另一个属性中具有相同的值,则更改对象的值 - JavaScript: Changing object value if another object has the same value in another property 将JavaScript对象属性附加到另一个对象 - Append JavaScript object property with another object Javascript Object Literal从另一个属性引用另一个属性 - Javascript Object Literal referring to another property in itself from another property 如何将 javascript 对象属性链接到同一对象的另一个属性? - How to link a javascript object property to another property of the same object? 'this'在另一个属性的javascript访问对象属性中的Object内未定义 - 'this' is undefined inside an Object in javascript accessing property of object by another property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM