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

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

継承方式と見やすさ

hacker's

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

hiyama's

/* test-inher-3-hiyama.js */
// コンストラクタ
var Point = function (x, y)
{
    this.x = x;
    this.y = y;
};
Point.prototype =
{
    moveTo : function (x, y)
    {
        this.x = x;
        this.y = y;
    },
    toString : function ()
    {
        return "(" + this.x + ", " + this.y + ")";
    }
};

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