2013-07-28 74 views
2

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

例如,

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 = {}; 
} 

但既不工作。

+0

分配给您的对象的*属性*,而不是一些变量。 – Bergi

回答

6

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

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

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