JavaScript継承パターンの間違った使い方
今日は意識せずに以下のようなコードを書いてしまって、30分ぐらいハマってしまった…。
function extend(s, c) { function f(){}; f.prototype = s.prototype; c.prototype = new f(); c.prototype.__super__ = s.prototype; c.prototype.__super__.constructor = s; c.prototype.constructor = c; return c; } var Foo = function() { // Fooのコンストラクタ }; var Bar = extend(Foo, function() { this.__super__.constructor(); }); Bar.prototype = { // Barのメソッド定義 }; var bar = new Bar(); // this.__super__が存在しないエラー
prototypeを書き換えちゃったら、そりゃthis.__super__が存在する訳ないですね…。
追記
ちなみにextendは以下のページのコードをほぼそのままコピペしてます。