Coffeescript code:
class Animal
constructor: (@name) ->
move: (meters) ->
alert @name + " moved #{meters}m."
class Snake extends Animal
move: ->
alert "Slithering..."
super 5
alert Snake instanceof Animal
Here is a link.
I really think this result true.
And my reason is this __extends method in compiled JavaScript:
__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;
};
child.prototype.prototype is parent.
Can someone tell me why?
And I know below is true:
alert new Snake('a') instanceof Animal
Your
Snakeis a subclass ofAnimal:That means that
Snake(a “class”) is actually an instance ofFunction, notAnimal. ASnakeobject, on the other hand, would be an instance ofAnimal:And if you try to get a
Snakeinstance to move:you’ll see that the right methods are called.
Demo: http://jsfiddle.net/ambiguous/3NmCZ/1/