起作用,我可以與其它功能類似所以它擴大:的javascript:加法功能如果我在「構造」添加在函數內部.prototype = {
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混亂 提及這一點。也許可以解決?
任何提示?謝謝!
做到這一點的唯一方法是後者;對象文字符號不支持該語法。 – Matt 2013-02-17 10:08:38
你爲什麼不創建第二堂課?然後只是做'this.time = new TimeFormatter();' – Prinzhorn 2013-02-17 10:08:42
這是Decorator模式的新方法:( – dfsq 2013-02-17 10:11:36