广州葆元健康生物科技有限公司


javascript 写类方式之八

网络编程 javascript 写类方式之八 06-22
8、Ext.js的写类方式
这里用的是Ext core3.0,Ext中用Ext.extend来定义一个类(当然它更多用来扩展一个类),Ext整个框架各种控件如Panel,MessageBox等都是用Ext.extend方法来扩展。这里仅仅用它来定义一个最简单的类。
看Ext.extend的代码可得知,它仍然是用构造函数和原型来组装一个类。
这里只需传两个参数即可,第一个参数是根类Object,第二个是原型。

/**
* Person类
* @param {Object} name
*/
var Person = Ext.extend(Object,{
constructor : function(name) {this.name = name;},
setName : function(name) {this.name = name;},
getName : function() {return this.name;}
});



//创建一个对象
var p = new Person("Lily");
console.log(p.getName());//Lily
p.setName("Andy");
console.log(p.getName());//Andy

//测试instanceof及p.constructor是否正确指向了Person
console.log(p instanceof Person);//true
console.log(p.constructor == Person);//true

比较特殊的是,如果单纯的定义一个类,那么第一个参数永远传Object即可。

javascript 写类方式之九
9、YUI的写类方式这里引入的是YUI2.7.0版,只需引入yahoo.js。YUI引入了命名空间,类似于java的包。以下yahoo的工具函数包YAHOO.namespaceYAHOO.langYAHOO.lang.hasOwnPro

javascript 写类方式之十
10、mootools.js的写类方式mootools.js的最新版本是1.2.3,这里使用的是1.2.0。mootool被设计成非常紧凑的,模块化的,面向对象的的js库。mootool中写类用Class类

javascript 面向对象思想 附源码
htmlheadscripttype="text/javascript"!--ClassModel=//类模型,用于创建类{create:function(){returnfunction(){this.construct.apply(this,arguments);}}}Extend=function(desc,src)//模拟类继承,


编辑:广州葆元健康生物科技有限公司

标签:的是,方式,第一个,只需,定义