转自:http://www.cnblogs.com/snandy/archive/2011/03/07/1972508.html
这篇我们看看各个JS库的写类方式,这也是写类系列的最后一篇。
1,Prototype的写类方式
Prototype中使用Class.create方法,如下
02 |
var Person = Class.create(); |
06 |
initialize : function (name) { |
09 |
getName : function () { |
12 |
setName : function (name) { |
18 |
var p = new Person( "jack" ); |
19 |
console.log(p.constructor == Person); |
initialize完成对象的初始化(相当于构造函数,必不可少),方法依次往下写即可。
有个问题,但是p.constructor == Person却为false,这正是Prototype库一个小缺陷。原因是重写了Person的原型。为了使constructor能指向正确的构造器,只需在原型重写时维护好constructor属性即可。
03 |
initialize : function (name) { |
06 |
getName : function () { |
09 |
setName : function (name) { |
2,Dojo的写类方式
dojo中用dojo.declare方法来定义一个类。dojo.declare有三个参数
参数1:类名className
参数2:继承的类superclass
参数3:构造器,方法props
单纯的定义一个类实际只需传第一,三两个参数。因为这里只讨论如何定义一个类,不讨论继承。
02 |
var className = "Person" ; |
05 |
constructor : function (name){ this .name=name;}, |
06 |
getName : function (){ return this .name;}, |
07 |
setName : function (name){ this .name = name;} |
11 |
dojo.declare(className, null ,proto); |
14 |
var p = new Person( "tom" ); |
15 |
console.log(p.getName()); |
17 |
console.log(p.getName()); |
20 |
console.log(p instanceof Person); |
21 |
console.log(p.constructor === Person); |
3,Ext的写类方式
Ext中用Ext.extend来定义一个类(当然它更多用来扩展一个类),Ext整个框架各种控件如Panel,MessageBox等都是用Ext.extend方法来扩展。这里仅仅用它来定义一个最简单的类。
这里只需传两个参数即可,第一个参数是根类Object,第二个是原型。比较特殊的是,如果单纯的定义一个类,那么第一个参数永远传Object即可。
05 |
var Person = Ext.extend(Object,{ |
06 |
constructor : function (name) { this .name = name;}, |
07 |
setName : function (name) { this .name = name;}, |
08 |
getName : function () { return this .name;} |
12 |
var p = new Person( "Lily" ); |
13 |
console.log(p.getName()); |
15 |
console.log(p.getName()); |
18 |
console.log(p instanceof Person); |
19 |
console.log(p.constructor == Person); |
4,YUI的写类方式
02 |
YAHOO.namespace( "test" ); |
05 |
YAHOO.test.Person = function (name) { |
08 |
YAHOO.test.Person.prototype.setName = function (name){ this .name = name;} |
09 |
YAHOO.test.Person.prototype.getName = function (){ return this .name;} |
13 |
var p = new YAHOO.test.Person( "jack" ); |
15 |
console.log(p.getName()); |
17 |
console.log(p.getName()); |
20 |
console.log(p instanceof YAHOO.test.Person); |
21 |
console.log(p.constructor == YAHOO.test.Person); |
可以看出除了多了包名,与原始的 构造函数+原型 方式 并无区别。
5,Mootools的写类方式
mootools是被设计成非常紧凑的,模块化的,面向对象的JS库。mootools中写类用Class类。导入后我们写类时只需要用Class就行了。
05 |
var Person = new Class({ |
06 |
initialize: function (name){ |
09 |
setName : function (name) { |
12 |
getName : function () { |
18 |
var p = new Person( "jack" ); |
21 |
console.log(p.getName()); |
23 |
console.log(p.getName()); |
26 |
console.log(p instanceof Person); |
27 |
console.log(p.constructor == Person); |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具