`
cobra
  • 浏览: 49517 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Class.create Vs Object.extend

阅读更多

Prototype在1.6之前的实现一个继承的操作的时候都是采用Object.extend的方式。Object.extend的实现原理是先创建出父类的对象并将子类中的方法复制到这个对象中去,这样做可以比较轻松的实现一个子类到父类的upcasting。但是这样也会存在一个问题,就是子类无法在自己的构造函数中调用父类的构造函数,也就是说无法实现super这样的操作。

1.6以前的Object.extend的实现代码:

  1. Object.extend = function(destination, source) {
  2. for (var property in source) {
  3. destination[property] = source[property];
  4. }
  5. return destination;
  6. }

1.6的Prototype中改写了关于继承的实现,Class.create新增加了一种模式的调用,Class.create([BassClass], {});

Prototypejs.org提供的Class.create新的实例:

  1. var Animal = Class.create({
  2. initialize: function(name, sound) {
  3. this.name = name;
  4. this.sound = sound;
  5. },
  6.  
  7. speak: function() {
  8. alert(this.name + " says: " + this.sound + "!");
  9. }
  10. });
  11.  
  12. // subclassing Animal
  13. var Snake = Class.create(Animal, {
  14. initialize: function($super, name) {
  15. $super(name, 'hissssssssss');
  16. }
  17. });
  18.  
  19. var ringneck = new Snake("Ringneck", "hissssssss");
  20. ringneck.speak();
  21.  
  22. //-> alerts "Ringneck says: hissssssss!"
  23.  

在这个实例中我们看到了$super这个新增加的函数,通过$super方法我们可以调用父类的方法。再来看一下最新的Prototype 1.6中的Class.create的实现:

  1. /* Based on Alex Arnell's inheritance implementation. */
  2. var Class = {
  3. create: function() {
  4. var parent = null, properties = $A(arguments);
  5. if (Object.isFunction(properties[0]))
  6. parent = properties.shift();
  7.  
  8. function klass() {
  9. this.initialize.apply(this, arguments);
  10. }
  11.  
  12. Object.extend(klass, Class.Methods);
  13. klass.superclass = parent;
  14. klass.subclasses = [];
  15.  
  16. if (parent) {
  17. var subclass = function() { };
  18. subclass.prototype = parent.prototype;
  19. klass.prototype = new subclass;
  20. parent.subclasses.push(klass);
  21. }
  22.  
  23. for (var i = 0; i < properties.length; i++)
  24. klass.addMethods(properties[i]);
  25.  
  26. if (!klass.prototype.initialize)
  27. klass.prototype.initialize = Prototype.emptyFunction;
  28.  
  29. klass.prototype.constructor = klass;
  30.  
  31. return klass;
  32. }
  33. };

可以看到最新的继承实现将父类暂存中superclass,这样在子类中通过$super调用父类的方法时,实际上调用的是this.superclass中的方法。

在开发Glove.Widget时候曾经就为没有super方法犯愁过,现在1.6有了新的继承模式Glove.Widget就不用像以前那样丑陋的实现super的功能了。明天开始考虑Glove.Widget基于Prototype1.6的重构。

分享到:
评论
1 楼 jeho0815 2011-03-04  
高手。。。

相关推荐

    Prototype使用指南之base.js

    base.js中包含下面的内容 类的创建与继承: Class.create(): 创建一个类,例如 person=Class.create() Object.extend(destination, source): 把source中方法属性copy到destination(使用for propertyin source),...

    CPPToolTip VC汽泡提示控件

    // 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...

    PROGRAMMING ACTIONSCRIPT 3.0

    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...

    js的日历时间控件

    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....

    jquery需要的所有js文件

    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....

    C# Game Programming Cookbook for Unity 3D - 2014

    4.2 The Timer Class....................................................43 4.2.1 Script Breakdown..........................................45 4.3 Spawn Scripts.............................................

    对于JS继承详细介绍( 原型链,构造函数,组合,原型式,寄生式,寄生组合,Class extends)

    JS中继承可以按照是否使用object函数(在下文中会提到),将继承分成两部分(Object.create是ES5新增的方法,用来规范化这个函数)。 其中,原型链继承和原型式继承有一样的优缺点,构造函数继承与寄生式继承也相互...

    Swift.3.Protocol-Oriented.Programming.2nd.Edition.epub

    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 ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    详解自定义ajax支持跨域组件封装

    Class.create()分析 仿prototype创建类继承 var Class = { create: function () { var c = function () { this.request.apply(this, arguments); } for (var i = 0, il = ... Object.extend(c.prototype,

    game-stack-object:GameStack 的基本可扩展对象类

    游戏堆栈对象GSObject 类允许您扩展现有类,创建...// Create a brand new classvar MyClass = GSObject . extend ( { init : function ( ) { // Some constructor stuff } , aUselessFunc = function ( pointless )

    Ext Js权威指南(.zip.001

    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 开始创建类 / ...

    Senfore_DragDrop_v4.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: ...

    Packt.Python.Journey.from.Novice.to.Expert.2016

    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 ...

    Visual C++ 编程资源大全(英文源码 数据库)

    (72KB)&lt;END&gt;&lt;br&gt;24,SerializeObjectIntoDBField.zip A class used to serialize object into a database field(95KB)&lt;END&gt;&lt;br&gt;25,xymessenger.zip Introducing a database tool for client-server systems...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    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....

    Visual Assist X 2107官方原版 带破解补丁

    [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+] ...

    UG6.0快捷键大全

    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... ...

    Visual C++ 编程资源大全(英文源码 控件)

    (14KB)&lt;END&gt;&lt;br&gt;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)&lt;END&gt;&lt;br&gt;43,splits.zip ...

Global site tag (gtag.js) - Google Analytics