在对象内部声明VAR会给我错误



我试图如下创建对象内部的变量,但这给了我错误。这是演示

和相同的代码在

以下
var One = {
    var var1, var2;
    init: function () {
      var1 = 'some1';
      var2 = 'some2';
    }        
}
One.init();

objecs具有属性而不是变量:

var One = {
    prop: "",
    prop2: "",
    init: function() {
       this.prop = 'some1';
       this.prop2 = 'some2';
    }        
}

对象应该像这样:

var Object = {
  /**
   * Documantation.
   */
  var1: null,
  /**
   * Documantation.
   */
  var2: null,
  /**
   * Documantation.
   */
  init: function () {
    this.var1 = 'some1';
    this.var2 = 'some1';
  }
};
Object.init();

但是,关于要完成的任务的更多信息将是很棒的,更清楚答案。

作为替代方案,您可以做到这一点:

function One(Var1, Var2)
{
    this.var1 = Var1;
    this.var2 = Var2;
}
var OneExample = new One('some1','some2');
var OneExampleVar1 = OneExample.var1;

编辑 - 这是小提琴演示

最新更新