class B extends A calls A.extended(B)

This commit is contained in:
Sam Stephenson
2010-07-18 14:22:07 -05:00
parent 989d539af3
commit 5678bf10fd
3 changed files with 26 additions and 2 deletions

View File

@@ -1668,7 +1668,7 @@
} }
}); });
UTILITIES = { UTILITIES = {
'extends': "function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.__superClass__ = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n }", 'extends': "function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n if (typeof parent.extended === \"function\") parent.extended(child);\n child.__superClass__ = parent.prototype;\n }",
hasProp: 'Object.prototype.hasOwnProperty', hasProp: 'Object.prototype.hasOwnProperty',
slice: 'Array.prototype.slice' slice: 'Array.prototype.slice'
}; };

View File

@@ -1479,9 +1479,10 @@ UTILITIES: {
function(child, parent) { function(child, parent) {
var ctor = function(){ }; var ctor = function(){ };
ctor.prototype = parent.prototype; ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor(); child.prototype = new ctor();
child.prototype.constructor = child; child.prototype.constructor = child;
if (typeof parent.extended === "function") parent.extended(child);
child.__superClass__ = parent.prototype;
} }
""" """

View File

@@ -183,3 +183,26 @@ c: new Child
c.method 1, 2, 3, 4 c.method 1, 2, 3, 4
ok c.args.join(' ') is '1 2 3 4' ok c.args.join(' ') is '1 2 3 4'
# Test `extended` callback.
class Base
@extended: (subclass) ->
for key, value of @
subclass[key]: value
class Element extends Base
@fromHTML: (html) ->
node: "..."
new @(node)
constructor: (node) ->
@node: node
ok Element.extended is Base.extended
ok Element.__superClass__ is Base.prototype
class MyElement extends Element
ok MyElement.extended is Base.extended
ok MyElement.fromHTML is Element.fromHTML
ok MyElement.__superClass__ is Element.prototype