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
ここにリンクがあります。
この結果は本当に正しいと思います。そして私の理由は__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