如果我在“构造函数”中添加一个函数,我可以使用其他函数来扩展它,如下所示:
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 混乱对此的引用。也许可以解决?
有小费吗?谢谢!