0
的CoffeeScript代码:为什么一个类不是它的父类的“instanceof”?
class Animal
constructor: (@name) ->
move: (meters) ->
alert @name + " moved #{meters}m."
class Snake extends Animal
move: ->
alert "Slithering..."
super 5
alert Snake instanceof Animal
这里是a link。
我真的认为这个结果是正确的。 我的原因是这样的__extends
方法在编译的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
是父母。
有人能告诉我为什么吗? 我下面知道是真是假:
alert new Snake('a') instanceof Animal