Created
October 25, 2012 06:29
-
-
Save chakrit/3950840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.3.3 | |
(function() { | |
var Base, Child, | |
__hasProp = {}.hasOwnProperty, | |
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | |
Base = (function(_super) { | |
__extends(Base, _super); | |
function Base() { | |
return Base.__super__.constructor.apply(this, arguments); | |
} | |
Base.prototype.key = 'base'; | |
return Base; | |
})(Object); | |
Child = (function(_super) { | |
__extends(Child, _super); | |
function Child() { | |
return Child.__super__.constructor.apply(this, arguments); | |
} | |
Child.prototype.key = 'child'; | |
return Child; | |
})(Base); | |
console.log((new Child()).key); | |
}).call(this); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Base extends Object | |
key: 'base' | |
class Child extends Base | |
key: 'child' | |
console.log (new Child()).key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Base() { } | |
Base.prototype = new Object(); | |
Base.prototype.key = 'base'; | |
function Child() { } | |
Child.prototype = new Base(); | |
Child.prototype.key = 'child'; | |
console.log( (new Child()).key ) |
The problem is CoffeeScript inheritance works in a pretty different way from the inheritance model mocked by plain JavaScript prototypes. Let me even further simplify your example:
key: 'base'
console.log (new Base()).key```
Even this one produces an`undefined`. In CoffeeScript, you should avoid extending Object, unless you know what you are doing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CoffeeScript output:
JavaScript output: