「コンストラクター」に関数を追加すると、次のように他の関数で拡張できます。
var MyClass = function() {
this.time = function() { return 4.5; }
this.time.formatted = function() { return format(this.time(), "HH:mm"); }
}
次のようにプロトタイプで関数を作成すると、これを行う良い方法がわかりません。
var MyClass = function() {}
MyClass.prototype = {
time: function() { return 4.5; },
time.formatted: function () { ... } // This does not work!
}
MyClass.prototype.time.formatted = function() { ... }
// the line above works but I don't like it since it separates everything.
// e.g. if I have 15 functions inside prototype, the .formatted will be like
// 50 lines apart from the time: function
*編集: * 再考すると、上記の行は機能しません。.formatted を追加すると、これへの参照が混乱します。もしかして解ける?
任意のヒント?ありがとう!