我正在使用jQuery,而我對JavaScript仍然很陌生。我在執行對象爲以下幾點:如何將na對象屬性重置爲默認值?
MyObject = {
properties : [{}],
resetProperties: function resetProperties() { this.properties = [{}] }
};
正如你可以在上面的代碼中看到我可以通過運行MyObject.resetProperties()
重置properties
但是,爲了做到這一點,我州倍[{}]
變量。我應該如何在不重複該代碼的情況下完成同樣的事情?
更新
我試着做到以下幾點:
MyObject = {
properties : this.propertiesDefault,
resetProperties : function resetProperties() { this.properties = [{}] },
propertiesDefault: [{}]
};
,但我得到 「TypeError: invalid 'in' operand MyObject.properties
」,我不知道這是正確的前進道路。
請參閱我的更新。 – user12882
其實,我不認爲這種克隆方法(即使用jQuery)會起作用。但是其他一些深度克隆方法應該適合您。也許是這樣的:JSON.parse(JSON.stringify(this.defaultProperties)); – null