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;
}
.
.
.
精彩评论