开发者

Javascript - property of an object in another property?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜