Prototype在1.6之前的实现一个继承的操作的时候都是采用Object.extend的方式。Object.extend的实现原理是先创建出父类的对象并将子类中的方法复制到这个对象中去,这样做可以比较轻松的实现一个子类到父类的upcasting。但是这样也会存在一个问题,就是子类无法在自己的构造函数中调用父类的构造函数,也就是说无法实现super这样的操作。
1.6以前的Object.extend的实现代码:
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
1.6的Prototype中改写了关于继承的实现,Class.create新增加了一种模式的调用,Class.create([BassClass], {});
Prototypejs.org提供的Class.create新的实例:
var Animal = Class.create({
initialize: function(name, sound) {
this.name = name;
this.sound = sound;
},
speak: function() {
alert(this.name + " says: " + this.sound + "!");
}
});
// subclassing Animal
var Snake = Class.create(Animal, {
initialize: function($super, name) {
$super(name, 'hissssssssss');
}
});
var ringneck = new Snake("Ringneck", "hissssssss");
ringneck.speak();
//-> alerts "Ringneck says: hissssssss!"
在这个实例中我们看到了$super这个新增加的函数,通过$super方法我们可以调用父类的方法。再来看一下最新的Prototype 1.6中的Class.create的实现:
/* Based on Alex Arnell's inheritance implementation. */
var Class = {
create: function() {
var parent = null, properties = $A(arguments);
if (Object.isFunction(properties[0]))
parent = properties.shift();
function klass() {
this.initialize.apply(this, arguments);
}
Object.extend(klass, Class.Methods);
klass.superclass = parent;
klass.subclasses = [];
if (parent) {
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
parent.subclasses.push(klass);
}
for (var i = 0; i < properties.length; i++)
klass.addMethods(properties[i]);
if (!klass.prototype.initialize)
klass.prototype.initialize = Prototype.emptyFunction;
klass.prototype.constructor = klass;
return klass;
}
};
可以看到最新的继承实现将父类暂存中superclass,这样在子类中通过$super调用父类的方法时,实际上调用的是this.superclass中的方法。
在开发Glove.Widget时候曾经就为没有super方法犯愁过,现在1.6有了新的继承模式Glove.Widget就不用像以前那样丑陋的实现super的功能了。明天开始考虑Glove.Widget基于Prototype1.6的重构。
分享到:
相关推荐
base.js中包含下面的内容 类的创建与继承: Class.create(): 创建一个类,例如 person=Class.create() Object.extend(destination, source): 把source中方法属性copy到destination(使用for propertyin source),...
// Create the CPPToolTip object m_tooltip.Create(this); 2.3 添加提示控件 m_tooltip.AddTool(GetDlgItem(IDC_BUTTON1), _T("Tooltip to the control IDC_BUTTON1")); 或者: m_tooltip.AddTool(this, _T("Tooltip...
Create a custom class and define methods to handle the callback methods.568 Extend the NetStream class and add methods to handle the callback methods569 Extend the NetStream class and make it dynamic...
if(typeof Control=="undefined"){Control={}}Control.DatePicker=Class.create({initialize:function(element,options){this.element=$(element);if(dp=this.element.retrieve("datepicker")){dp.destroy()}this....
a.extend.apply(null,[!0,e].concat(g)):e;if(f&&e.charAt(0)==="_")return h;f?this.each(function(){var d=a.data(this,c),f=d&&a.isFunction(d[e])?d[e].apply(d,g):d;if(f!==d&&f!==b){h=f;return!1}}):this....
4.2 The Timer Class....................................................43 4.2.1 Script Breakdown..........................................45 4.3 Spawn Scripts.............................................
JS中继承可以按照是否使用object函数(在下文中会提到),将继承分成两部分(Object.create是ES5新增的方法,用来规范化这个函数)。 其中,原型链继承和原型式继承有一样的优缺点,构造函数继承与寄生式继承也相互...
Create a flexible codebase with protocols and protocol extensions Increase the overall productivity and performance of applications with protocol-oriented programming Book Description One of the most ...
For example, if your header file uses the File class in ways that do not require access to the declaration of the File class, your header file can just forward declare class File; instead of having ...
Class.create()分析 仿prototype创建类继承 var Class = { create: function () { var c = function () { this.request.apply(this, arguments); } for (var i = 0, il = ... Object.extend(c.prototype,
游戏堆栈对象GSObject 类允许您扩展现有类,创建...// Create a brand new classvar MyClass = GSObject . extend ( { init : function ( ) { // Some constructor stuff } , aUselessFunc = function ( pointless )
4.3.2 ext.object中的静态方法 / 113 4.3.3 ext.function中的静态方法 / 120 4.3.4 ext.array中的静态方法 / 127 4.3.5 ext.error中的静态方法 / 133 4.4 深入了解类的创建及管理 / 135 4.4.1 开始创建类 / ...
Drag and Drop Component Suite Version 4.1 Field test 5, released 16-dec-2001 ?1997-2001 Angus Johnson & Anders Melander ... ------------------------------------------- Table of Contents: ...
Extend class functionality using inheritance Exploit object-oriented programming in key Python technologies, such as Kivy and Django Understand how and when to use the functional programming paradigm ...
(72KB)<END><br>24,SerializeObjectIntoDBField.zip A class used to serialize object into a database field(95KB)<END><br>25,xymessenger.zip Introducing a database tool for client-server systems...
From there, you’ll create a custom LLDB command which gives you the stack trace of when an object was allocated or deallocated in memory — even after the stack trace is long gone from the debugger....
[VS2015 Update 3] When changing the state of the automatically extend multi-line comments setting, VA prompts to make the opposite action with the IDE provided feature in C#. (case=95605) [VS2012+] ...
BITMAP blank_object POPUP_LABEL Hide P&arents POPUP_MESSAGE Hides the parents of the selected feature. ACTIONS STANDARD BUTTON/HIDE UG_MODELING_EDIT_SKETCH_PROFILE LABEL Edit S&ketch... ...
(14KB)<END><br>42,spinrange.zip SpinRange is a VC++ 6.0 project that shows how to subclass a CSpinButtonControl to dramatically extend the range of the control. (24KB)<END><br>43,splits.zip ...