JavaScript的写类方式(5)
这篇我们看看各个JS库的写类方式,这也是写类系列的最后一篇。
1,Prototype的写类方式
Prototype中使用Class.create方法,如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //类名Person var Person = Class.create(); // 通过原型重写来定义Person Person.prototype = { initialize : function (name) { this .name = name; }, getName : function () { return this .name; }, setName : function (name) { this .name = name; } } // 创建对象 var p = new Person( "jack" ); console.log(p.constructor == Person); //false |
initialize完成对象的初始化(相当于构造函数,必不可少),方法依次往下写即可。
有个问题,但是p.constructor == Person却为false,这正是Prototype库一个小缺陷。原因是重写了Person的原型。为了使constructor能指向正确的构造器,只需在原型重写时维护好constructor属性即可。
1 2 3 4 5 6 7 8 9 10 11 12 | Person.prototype = { constructor : Person, // 注意这句将修复constructor属性 initialize : function (name) { this .name = name; }, getName : function () { return this .name; }, setName : function (name) { this .name = name; } } |
2,Dojo的写类方式
dojo中用dojo.declare方法来定义一个类。dojo.declare有三个参数
参数1:类名className
参数2:继承的类superclass
参数3:构造器,方法props
单纯的定义一个类实际只需传第一,三两个参数。因为这里只讨论如何定义一个类,不讨论继承。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // 定义类名 var className = "Person" ; // 定义构造器及方法 var proto = { constructor : function (name){ this .name=name;}, getName : function (){ return this .name;}, setName : function (name){ this .name = name;} } // 定义类Person dojo.declare(className, null ,proto); // 创建一个对象 var p = new Person( "tom" ); console.log(p.getName()); //tom p.setName( "jack" ); console.log(p.getName()); //jack // 测试instanceof及p.constructor是否正确指向了Person console.log(p instanceof Person); //true console.log(p.constructor === Person); //true |
3,Ext的写类方式
Ext中用Ext.extend来定义一个类(当然它更多用来扩展一个类),Ext整个框架各种控件如Panel,MessageBox等都是用Ext.extend方法来扩展。这里仅仅用它来定义一个最简单的类。
这里只需传两个参数即可,第一个参数是根类Object,第二个是原型。比较特殊的是,如果单纯的定义一个类,那么第一个参数永远传Object即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * 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 |
4,YUI的写类方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //定义包名 YAHOO.namespace( "test" ); // 定义类 YAHOO.test.Person = function (name) { this .name = name; } YAHOO.test.Person.prototype.setName = function (name){ this .name = name;} YAHOO.test.Person.prototype.getName = function (){ return this .name;} // 创建一个对象 var p = new YAHOO.test.Person( "jack" ); console.log(p.getName()); //jack p.setName( 'tom' ); console.log(p.getName()); //tom // 测试instanceof及p.constructor是否正确指向了YAHOO.test.Person console.log(p instanceof YAHOO.test.Person); console.log(p.constructor == YAHOO.test.Person); |
可以看出除了多了包名,与原始的 构造函数+原型 方式 并无区别。
5,Mootools的写类方式
mootools是被设计成非常紧凑的,模块化的,面向对象的JS库。mootools中写类用Class类。导入后我们写类时只需要用Class就行了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /** * Person类 * @param {Object} name */ var Person = new Class({ initialize: function (name){ this .name = name; }, setName : function (name) { this .name = name; }, getName : function () { return this .name; } }) // new一个对象 var p = new Person( "jack" ); // 测试set,get方法 console.log(p.getName()); //jac p.setName( 'andy' ); console.log(p.getName()); //andy // 测试instanceof及p.constructor是否正确指向了Person console.log(p instanceof Person); //true console.log(p.constructor == Person); //true |
可以继续阅读:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端