1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

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 = {
'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',
slice: 'Array.prototype.slice'
};

View file

@ -1479,9 +1479,10 @@ UTILITIES: {
function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor();
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
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