开发者

Javascript - property of an object in another property?

开发者 https://www.devze.com 2023-01-16 11:06 出处:网络
In the following example, how do I modify the varia开发者_运维技巧ble \"sizeMod\", which is a property of each object?

In the following example, how do I modify the varia开发者_运维技巧ble "sizeMod", which is a property of each object?

// Will be created by ids
var objs = {};

// Set up objects
$.each(ids, function(index, value){
    objs[value] = {
        sizeMod: 0
    };
    objs[value] = {
        change: function(){
            sizeMod += 1; // How do I write this line correctly?
        }
    };
});


the second statement is destroying your original object where sizeMod is declared.

this should work (untested)

$.each(ids, function(index, value){
    objs[value] = {
        sizeMod: 0,
        change: function(){
            this.sizeMod += 1; // How do I write this line correctly?
        }
    };
});

you may call as

objs['relevantkey'].change();


see if this helps:

.
.
.
objs[value] = {
    sizeMod: 0
};
objs[value].change = function(){
    this.sizeMod += 1;
}
.
.
.
0

精彩评论

暂无评论...
验证码 换一张
取 消