__extends関数を整形して、コメントを追加した。
ctorオブジェクトのところを理解するのにわりと苦労した。
あとconstructorプロパティの存在を知らなかったので、なんでこれで child.__super__.constructor() が出来るのかかなり悩んでしまった。結論としては、parent.prototype.constructor()がjavascriptの仕様として存在するというものだったが。
__hasProp = {}.hasOwnProperty;
__extends = function(child, parent) {
// parentのプロパティを、childにコピー。staticなメンバの継承用。
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
// コンストラクタを実行しないこと以外parentと同じ、継承用parentオブジェクトを作成。
// prototypeはparentのもので上書きするので、そのままだとprototype.constructorがparentを指してしまう。
// それを補正するために最初にconstructorプロパティを書き変える処理を入れている
// ctor.prototype.constructorを書き変えちゃうと一緒にparent.prototype.constructorが変わってしまうので、
// インスタンスのconstructorメンバを設定してprototypeを汚さないようにしている。
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
// 継承用parentオブジェクトをprototypeとして設定する。
child.prototype = new ctor();
// __super__というプロパティから、親クラスのメソッドを参照できるようにする
child.__super__ = parent.prototype;
// 完成したクラスを戻す
return child;
};