2007/10/22 | 使用SharedObjects存储自定义的类(转帖)
类别(转帖) | 评论(0) | 阅读(33) | 发表于 22:34
在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 doesn't 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
0

评论Comments

日志分类
首页[19]
我的日志[2]
个人FLASH[4]
色彩与图形[3]
PS与CD[0]
PHP.MYSQL[0]
转帖[10]