このブログは、旧・はてなダイアリー「檜山正幸のキマイラ飼育記 メモ編」(http://d.hatena.ne.jp/m-hiyama-memo/)のデータを移行・保存したものであり、今後(2019年1月以降)更新の予定はありません。

今後の更新は、新しいブログ http://m-hiyama-memo.hatenablog.com/ で行います。

親のものを乗っ取ることは、継承とは言わないかもね

/* test-inher-1.js */

// コンストラクタ
function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.moveTo = function (x, y) {
  this.x = x;
  this.y = y;
};

Point.prototype.toString = function () {
  return "(" + this.x + ", " + this.y + ")";
};

/* サブクラス?? */

// コンストラクタ
function ColoredPoint(x, y, color) {
  this.x = x;
  this.y = y;
  this.color = color;
}
// 継承??
ColoredPoint.prototype = Point.prototype;
// サブクラスのメソッド定義
ColoredPoint.prototype.setColor = function(color) {
  this.color = color;
};
ColoredPoint.prototype.toString = function() {
  return "(" + this.x + ", " + this.y + "; " + this.color + ")";
};
/* test-inher-2.js */

// コンストラクタ
function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.moveTo = function (x, y) {
  this.x = x;
  this.y = y;
};

Point.prototype.toString = function () {
  return "(" + this.x + ", " + this.y + ")";
};

/* サブクラス */

// コンストラクタ
function ColoredPoint(x, y, color) {
  this.x = x;
  this.y = y;
  this.color = color;
}
// 継承
ColoredPoint.prototype.__proto__ = Point.prototype;
// サブクラスのメソッド定義
ColoredPoint.prototype.setColor = function(color) {
  this.color = color;
};
ColoredPoint.prototype.toString = function() {
  return "(" + this.x + ", " + this.y + "; " + this.color + ")";
};