You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varparent=CLASS("parent",function(args){this.name=args.name;this.job=args.job;}).extend({say : function(){console.log("Hello, My name is "+this.name)}});varp=newparent({name : "chen",job : "web"});p.say()//Hello, My name is chen
varson=CLASS("son",function(args){// 每个子类都会有一个 _super_ 属性指向父类本身this.constructor._super_.call(this,arguments)}).base("parent");varp=newson({name : "simple",job : "web develop"});p.say()//Hello, My name is simple
上面定义了一个 son 类,继承了 parent 的方法跟属性,主要通过 base 接口去继承父类的方法
注:不考虑多父类继承,我能想到的原因很简单,javascript 中通过 new 关键字去实例化时,得到的对象中会有个隐性属性如 :proto 指向父类的原型;如果多继承,那这个属性该指向谁?
最近真的好闲,也是好事,可以抽空写写blog,分享个简单的 “类” 包装器。宗旨:让类的定义,继承,扩展更简单。
先来看看API够不够明了:
1、定义一个类
CLASS( "parent" );
最简单的一个 parent 类,没有属性,没有方法,没有构造函数;”裸类“
下面定义一个复杂的,有属性、有方法、有构造函数
这样,parent类中就拥有了自己的属性,方法,构造函数;主要通过 extend 接口去扩展方法,构造函数可有可无;
再来看看继承的API;定义一个 son 类去继承它吧。
上面定义了一个 son 类,继承了 parent 的方法跟属性,主要通过 base 接口去继承父类的方法
注:不考虑多父类继承,我能想到的原因很简单,javascript 中通过 new 关键字去实例化时,得到的对象中会有个隐性属性如 :proto 指向父类的原型;如果多继承,那这个属性该指向谁?
(属性的继承方式不是很友好,因为我实在没有更好的方法去包装这个接口,可能是实现的思路的一个缺陷。);
当 son 继承了 parent 之后,son 想去扩展自身的方法呢?
这样 son 就扩展了两个自己的新方法;
接口差不多就这些了,下面介绍下内部属性:
内部有存储以定义过的 类,不重复定义,可以重写;
每个类都拥有两个静态方法一个:base 和 extend ,主要是继承跟扩展的;
子类都会有一个 super 属性指向父类自身(继承父类属性的时候会用到);
每个类都会拥有一个 index 属性,记录该类在cache中的地址;平时用处不大,主要方便firebug调试。
请用Firefox打开 >>DEMO<<
The text was updated successfully, but these errors were encountered: