追記20131014
此の記事は間違ってゐます。以下を使ってください。function SpC(arg) { // Some codes. } SpC.prototype.f = function() { }; function SbC(arg) { Spc.apply(this, arguments); // Some codes. } SbC.prototype = Object.create(SpC.prototype); SbC.prototype.constructor = SbC; SbC.prototype.f = function() { };もう直ぐES6のmodule, classも来ますけどね。
覺え書き。
SpC
をSbC
へprototype継承させる。SpC
のthis
(prototype
でない) の値を使う場合、文字列や数値等の即値ならば、this.spProp = 'sp_prpp';
と直接初期化すれば好いが、配列やObject等の参照値であった場合は、遅延初期化を行う。
function SpC (arg) { this.spArg = arg || 'UNDEFINED'; this.spProp = 'sp_prop'; } Object.defineProperty(SpC.prototype, 'spHash', { get: function () { var initValue = {a: 1}; Object.defineProperty(this, 'spHash', {value: initValue}); return initValue; } }); SpC.prototype.spArgProt = function () { return this.spArg; }; function SbC (arg) { this.sbArg = arg || 'UNDEFINED'; this.sbProp = 'sb_prop'; } SbC.prototype = new SpC(); SbC.prototype.sbArgProt = function () { return this.sbArg; }; var sb1 = new SbC('sb1'); var sb2 = new SbC('sb2');
此れで大体上手くいく。