Skip to content

Instantly share code, notes, and snippets.

@edom18
Created October 16, 2012 14:55
Show Gist options
  • Save edom18/3899789 to your computer and use it in GitHub Desktop.
Save edom18/3899789 to your computer and use it in GitHub Desktop.
JSで継承をする仕組み。this._super() だけで親クラスのコンストラクタを呼びたい、ってことで作成。若干キモいけどいちおうは動く・・けど、もっとスマートにできないものか・・。
(function (win, doc, exports) {
'use strict';
/**
* クラス継承用関数
* child ... 派生クラスを指定
* _super ... 基底クラスを指定
*/
function extend(child, _super) {
function ctor() { this.constructor = child; }
ctor.prototype = _super.prototype;
child.prototype = new ctor();
child.prototype._superclass = _super;
child.prototype._super = function () {
this._prevctor = (this._prevctor) ? this._prevctor.prototype._superclass : _super;
this._prevctor.apply(this, arguments);
};
}
/*! ------------------------------------------------------------------------
内部で定義したクラスを外部から参照できるようにExport
---------------------------------------------------------------------------- */
exports.extend = extend;
}(window, document, window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment