tddjs > ダメな継承
by s_hiroshi
JavaScript
function Parent() {}
Parent.prototype.foo = function() {};
function Child() {}
Child.prototype = Parent.prototype; // オブジェクト参照が渡される。Child.prototypeの設定はParent.prototypeを書き換える。
Child.prototype.bar = function() {
console.log('bar');
}
var parent = new Parent();
parent.bar(); // 実行できる。
console.log(parent instanceof Child) // true;