主要试验了Mootools在OO开发方面的Class、JSON、Object对象的使用,在mootools在js的模块化开发方面较RequireJS和DOJO还是比较简便的。
在项目中只引用了mootools的基础类库,没有引用mootools的插件库,基础类库比较小,大约27KB,见附件。
mootools文档地址:http://mootools.net/docs/core
实例:
var Person = new Class({
//构造函数
initialize:function(name){
this.name = name ;
}
});
//直接扩展Person类,添加新的方法
Person.implement({
action : function(){
alert("run");
}
});
//类的继承机制
var Student = new Class({
Extends: Person,
initialize:function(name,age){
//调用父类的initialize方法
this.parent(name);
this.age = age ;
}
});
//通过implements创建新的对象,覆盖baseclass中已有的方法,也可以添加新的方法
var SuperMan = new Class({
Implements:Person,
action : function(){
alert("fly");
}
});
var p = new Person("lp");
var student = new Student("stu",27) ;
//alert(student.name+" "+student.age) ;
var man = new SuperMan("superman");
//alert(JSON.encode(man));
//alert(man.name);
//man.action();
//p.action();
var firstObj = {
name : 'lp'
} ;
var secondObj = {
name : 'xhy',
age : 27
};
//将secondObj追加到firstObj中
Object.append(firstObj,secondObj);
alert(JSON.encode(firstObj)+"\n"+JSON.encode(secondObj));
?
?
