1

我想通过称为此代码的方法更改对象的属性:

MAP = MAP || {
   level: 0
   updateProperty(property, value){
        if (this.hasOwnProperty(property){
          this.property = value
        }
   }
}

但是当我触发这段代码时,我会在我的对象中创建名为“property”的新属性。但我想将“财产”视为变量,而不是新财产!

4

1 回答 1

3

你可能想要这个(方括号this[property] = value;

var MAP = MAP || {};
MAP = {
    level: 0,
    updateProperty(property, value) {
        if (this.hasOwnProperty(property)) {
            this[property] = value;
        }
    }
};
于 2012-07-04T13:44:10.737 回答