内部javascript清除功能

时间:2013-07-28 17:32:43

标签: javascript

我有两个用于复制数据对象的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 = {};
}

但都不起作用。

1 个答案:

答案 0 :(得分:6)

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

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

  save : saveStrategyHandler,
  clear : function() {
    this.id = "";
    this.label = "";
    this.dueDate = "";
    this.comments = [];
  }
}