【问题标题】:Creating Object Properties in JSON (Javascript) Dynamically in a Method在方法中动态创建 JSON (Javascript) 中的对象属性
【发布时间】:2013-06-11 18:22:04
【问题描述】:

我在这里摸不着头脑。我对 JSON 和 Javascript 都很陌生,所以我想知道该怎么做。

假设我有一个对象:

MyObject.prototype = {
    // different methods and properties
    _randomMethod: function MyObject_randomMethod() {
         MyObject.myArray = [];
    },
};

如何为我的对象声明一个数组属性(如上图:MyObject.myArray = [];)并使其在整个对象中可用,以便我可以在其他方法中访问它。

也许这已经涵盖了,我只是没有使用正确的术语,但如果有人可以帮助我,我将不胜感激,因为我自己无法弄清楚。

很清楚,我想在类似上面示例的方法中动态声明此数组属性,然后能够在具有“this”引用或类似内容的同一对象内的其他方法中使用它。

【问题讨论】:

    标签: javascript arrays json object properties


    【解决方案1】:

    使用this 引用当前实例:

    MyObject.prototype = {
        // different methods and properties
        _randomMethod: function MyObject_randomMethod() {
             this.myArray = [];
        },
    };
    

    http://jsfiddle.net/5jSe3/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2016-01-09
      相关资源
      最近更新 更多