class
Inherit
copy
clone
extend
More than 5 years have passed since last update.

クラシカルなクラス継承を実現するサンプル。

backbone.jsを元にしてます。

(function (win, doc, exports) {

var ctor = function () {};

function copyClone(obj) {

var args = Array.prototype.slice.call(arguments, 1),
l = args.length,
i = 0,
src, prop;

for (; i < l; i++) {
src = args[i];
for (prop in src) {
obj[prop] = args[i][prop];
}
}

return obj;
}

function inherit(_super, protoProps, staticProps) {

var child;

//copy constructor to `child` if it has constructor property as own property.
if (protoProps && Object.prototype.hasOwnProperty.call(protoProps, 'constructor')) {
child = protoProps.constructor;
}

//else, child is function as to apply `_super`.
else {
child = function () {
_super.apply(this, arguments);
};
}

//copy _super property to child.
copyClone(child, _super);

//store the temporary _super.prototype in ctor.prototype.
ctor.prototype = _super.prototype;

//create instance to child.prototype as new ctor.
child.prototype = new ctor();

//if the protoProps is defined, copy it to child.
if (protoProps) {
copyClone(child.prototype, protoProps);
}

//if the staticProps is defined, copy it to child.
if (staticProps) {
copyClone(child, staticProp);
}

//child.prototype.constructor is child.
child.prototype.constructor = child;

//Finally, _super.prototype is stored in child.__super__ property.
child.__super__ = _super.prototype;

return child;
}

/* ------------------------------------------------------------------
EXPORTS
--------------------------------------------------------------------- */

exports.inherit = inherit;
exports.copyClone = copyClone;

}(window, document, window));