Skip to content

Instantly share code, notes, and snippets.

@chakrit
Created October 25, 2012 06:29
Show Gist options
  • Save chakrit/3950840 to your computer and use it in GitHub Desktop.
Save chakrit/3950840 to your computer and use it in GitHub Desktop.
// 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);
class Base extends Object
key: 'base'
class Child extends Base
key: 'child'
console.log (new Child()).key
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 )
@chakrit
Copy link
Author

chakrit commented Oct 25, 2012

CoffeeScript output:

undefined

JavaScript output:

child

@vy
Copy link

vy commented Oct 25, 2012

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