2013-07-28 76 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 = []; 
    } 
}