在Macromedia的文档中可以知道,SharedObjects只能存储基本的ActionScript或者javascript类型(Array,
Number, Boolean, String等),其实也可以使用SharedObjects来存储用户自己定义的类,而且可以使用几乎全部方法和变量。erik提供了一个方法:
/** Example of saving classes to shared objects */ // Person Class / Prototype Object function Person(p_sex){ // not saved to SharedObject this.m_sex = p_sex; } Person.prototype.setName = function(p_name){ this.m_name = p_name; } Person.prototype.setAge = function(p_age){ this.m_age = p_age; } Person.prototype.getName = function(){ trace("Name: "+this.m_name); } Person.prototype.getAge = function(){ trace("Age: "+this.m_age); } Person.prototype.getSex = function(){ trace("Sex: "+this.m_sex); } // Deserilizes custom class in ShareObject Object.registerClass("Person",Person); // Initializes SharedObject var SavedData_so = SharedObject.getLocal("PersonData"); // Creates and saves SharedObject if it doesnt already exist if(SavedData_so.data.Person == null){ trace("SharedObject Created"); // Parameters in constructor are NOT saved SavedData_so.data.Person = new Person("male"); SavedData_so.data.Person.setName("Erik"); SavedData_so.data.Person.setAge(25); SavedData_so.flush(); }else{ trace("SharedObject Retrieved"); } // Call SharedObjects custom class methods SavedData_so.data.Person.getName(); SavedData_so.data.Person.getAge(); SavedData_so.data.Person.getSex(); // Output // First Run: Creates / Saves SharedObject Created Name: Erik Age: 25 Sex: male // Second Run Retrieves SharedObject // as you can see, parameters specified in the constructor are not saved correctly. SharedObject Retrieved Name: Erik Age: 25 Sex: undefined // Insert joke here |
原文:http://www.erikbianchi.com/archives/2004/11/flash_quirks_sa.html
[From Riacn]