2

我有两个用于复制数据对象的 javascript 对象。它们是通过 onclick 事件填充的,我想在保存事件后清除它们。

例如,

var currentStrategy = {
  id : "",
  label : "",
  dueDate : "",
  comments = [],

  save : saveStrategyHandler,
  clear : function() {
    //how do I clear the fields above in here?
  }
}

我试过了

function(){
  id = "";
  label = "";
  dueDate = "";
  comments = [];
}

function(){
  currentStrategy = {};
}

但两者都不起作用。

4

1 回答 1

6

使用以下内容。实例属性需要一个this.

var currentStrategy = {
  id : "",
  label : "",
  dueDate : "",
  comments = [],

  save : saveStrategyHandler,
  clear : function() {
    this.id = "";
    this.label = "";
    this.dueDate = "";
    this.comments = [];
  }
}
于 2013-07-28T17:34:49.157 回答