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
varHumen=Class.create({initialize: function(name,age,gender){this.age=age;this.name=name;this.gender=gender;},eat: function(){console.log('I eat normal food !');},sleep: function(){}});varSuperMen=Class.create(Humen,{initialize: function($super,name,age,gender,power){$super(name,age,gender,power);},eat: function($super){$super();console.log('I am '+this.name+', I still can eat gasoil, can you ?');}})varsm=newSuperMen('supermen');sm.eat();
结果如下:
I eat normal food ! general.js:9
I am supermen, I still can eat gasoil, can you ?
本文假定你有了一定的javascript面向对象的基础了,就不过多的关于javascript面向对象基础的介绍。
首先,让我们来举个例子,做个传统意义上的javascript的继承。
OK, 初步完成了一个“人类”和一个“超人”的基本结构了,下面让我们看下这段对“人类”的描述代码,在结构上究竟有什么不足呢?
下面让我们看下prototype.js对这段代码的重新定义
结果如下:
I eat normal food ! general.js:9
I am supermen, I still can eat gasoil, can you ?
嗯,让我们现在再来做个对比,看看prototype.js中OOP的继承部分的大概结构。
prototype.js定义类的方法很简单,比起原始的js用function来定义而言,引入了Class类别。具体可以看这里。
initialize方法定义了这个类的构造函数,初始化类的基本数据。这里,值得关注的是构造方法中的第一个参数$super,如果父类中有同样的方法,$super则表示父类中同名的函数。这样一来,是不是就可以调用父类的方法了呢。而不至于造成代码的冗余。
在prototype.js的类中,有两个比较特别的参数superclasses,subclasses。分别表示了当前类的所有的子类,或者是所有的父类。这样一来,在当前的类中,我们就不不止可以操作父类中的方法,而且祖宗八倍的方法都可以调用了
是不是很棒!?
prototype.js异于jQuery的最明显的区别,就在于设计的理念不一样了,jQuery崇尚的是select and do,而在Prototype.js里,体现的更多的是一切皆是对象的思想。
prototype.js胜于jQuery的一步就在于它在定义了一些工具类方法,屏蔽了浏览器差异的同时,也为灵活的js规定了一套编程的规范,代码的规整,类的封装等等。使用这套框架的开发人员都按照这样的规则去写,代码就不至于显得太凌乱。
转载请注明出处
botobe.net
本文Github链接
2012.08.13
一切安好。
Merci !
The text was updated successfully, but these errors were encountered: