c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

JavaScriptのprototype継承実験覺え書き

追記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も来ますけどね。


覺え書き。SpCSbCへprototype継承させる。
SpCthis (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');

此れで大体上手くいく。