函数不能序列化为JSON对象。
因此,我建议您为实际属性创建一个单独的对象(或对象内的属性),然后序列化此部分。之后,您可以使用其所有功能实例化对象,并重新应用所有属性以重新获得对工作对象的访问权限。
按照您的示例,它可能看起来像这样:
function MyObject() { this.init(); }MyObject.prototype = { data: { property1: "", property2: "" }, init: function () { this.property1 = "First"; this.property2 = "Second"; }, test: function() { alert("Executing test!"); }, save: function( id ) { window.localStorage.setItem( id, JSON.stringify(this.data)); }, load: function( id ) { this.data = JSON.parse( window.getItem( id ) ); }}


