Coffeescript 为什么一个类不是一个“类”;instanceof";它的父类?
咖啡脚本代码:Coffeescript 为什么一个类不是一个“类”;instanceof";它的父类?,coffeescript,Coffeescript,咖啡脚本代码: class Animal constructor: (@name) -> move: (meters) -> alert @name + " moved #{meters}m." class Snake extends Animal move: -> alert "Slithering..." super 5 alert Snake instanceof Animal 这是 我真的认为这个结果是真的。 我的理由是这个\
class Animal
constructor: (@name) ->
move: (meters) ->
alert @name + " moved #{meters}m."
class Snake extends Animal
move: ->
alert "Slithering..."
super 5
alert Snake instanceof Animal
这是
我真的认为这个结果是真的。
我的理由是这个\u扩展了编译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
你的蛇
是动物
的一个子类:
class Snake extends Animal
alert Snake instanceof Function # true
alert (new Snake) instanceof Animal # true
这意味着蛇
(一个“类”)实际上是函数
的一个实例,而不是动物
。另一方面,蛇
对象将是动物
的实例:
class Snake extends Animal
alert Snake instanceof Function # true
alert (new Snake) instanceof Animal # true
如果您试图让一条Snake
实例移动:
(new Snake('Pancakes')).move()
您将看到调用了正确的方法
演示:你的蛇
是动物
的一个子类:
class Snake extends Animal
alert Snake instanceof Function # true
alert (new Snake) instanceof Animal # true
这意味着蛇
(一个“类”)实际上是函数
的一个实例,而不是动物
。另一方面,蛇
对象将是动物
的实例:
class Snake extends Animal
alert Snake instanceof Function # true
alert (new Snake) instanceof Animal # true
如果您试图让一条Snake
实例移动:
(new Snake('Pancakes')).move()
您将看到调用了正确的方法
演示: