Helma Logo
main list history

Version 6 by zumbrunn on 26. March 2007, 15:53

24    var that = arguments.callee.caller.__parent || this;
26        if (that.prototype[method] && that.prototype[method] != obj[method])obj[method]
27          return     && that.prototype[method]prototype[method] != arguments.apply(obj,args);callee.caller)
28       that =     that.prototypeprototype[method].constructor;__parent = that;
29            return that.prototype[method].apply(obj,args);
30        }
31        that = that.prototype.constructor;

Version 5 by zumbrunn on 15. January 2007, 09:41

11 Function.prototype.inherit = function(inherit) function(fnc)
12    var fnc constr = inherit ? this : function(){};this;
13    inherit = inherit || this;
13       inherit.apply(this, arguments);if (fnc)
14            fnc.apply(this, arguments);
15        constr.apply(this, arguments);
15    Constructor.prototype = new inherit();(fnc || this)();
16    Constructor.prototype.constructor = inherit;fnc || this;

Version 4 by zumbrunn on 13. January 2007, 11:19

20    Constructor.prototype.inherit = fnc.prototype;

Version 3 by zumbrunn on 12. January 2007, 19:13

28          return that.prototype[method].apply(obj,args);

Version 2 by zumbrunn on 12. January 2007, 13:18

23 
34 

Version 1 by zumbrunn on 12. January 2007, 13:17

1In its current incarnation, Javascript prototype inheritance has some shortcomings. In particular, prototypes do not inherit their parent's constructor and methods in child prototypes can not dynamically access an inherited method they override.
2
3After *Hannes'|JavaScript Inheritance Sugar* and *Jürg's|JavaScript Inheritance Sugar - Lehni* suggestions for improvements to Dean Edwards' *Base Class|http://dean.edwards.name/weblog/2006/03/base/* approach to Javascript inheritance, I made an attempt to solve the problem by focusing more intensely on fixing Javascript's prototype inheritance than adding class sugar around it.
4
5What I came up with is this:
6
7<% this.attachments %>
8
9Mocha Inheritance does its magic in these 20 lines of code:
10
11 Function.prototype.inherit = function(inherit) {
12    var fnc = inherit ? this : function(){};
13    inherit = inherit || this;
14    var Constructor = function(){
15      inherit.apply(this, arguments);
16      fnc.apply(this, arguments);
17    };
18    Constructor.prototype = new inherit();
19    Constructor.prototype.constructor = inherit;
20    Constructor.prototype.inherit = fnc.prototype;
21    return Constructor;
22 };
23
24 Function.prototype.applySuper = function(method,obj,args) {
25    var that = this;
26    do {
27      if (that.prototype[method] && that.prototype[method] != obj[method])
28          that.prototype[method].apply(obj,args);
29      that = that.prototype.constructor;
30    } while (that != Object);
31 };
32
33Creating a new prototype "Mensch" with mocha inheritance from "Animal":
34 
35 var Mensch = Animal.inherit();
36
37Letting a previously created constructor "Mensch" mocha inherit from "Animal":
38
39 Mensch = Mensch.inherit(Animal);
40
41And here an example of a Mensch method "evolve" that overrides what it inherits from Animal, but still manages to do nothing different:
42
43 Mensch.prototype.evolve = function(){
44  return this.constructor.applySuper('evolve', this, arguments);
45 }
46
47With some more syntactic sugar around it, I think this is quite a nice approach.
48