2
林試圖寫一個包裝用於鏈式方法:包裝保存方法的結果
new Example().child("First").child("Second").read(function(name){console.log(name)})
該包裝應該保存的第一執行的結果。下一次包裝被調用它不應該執行原來的方法同樣的方法,它應該返回所保存的結果:
wrapper = new Wrapper();
//this time the result of the passed method gets saved
wrapper.wrap(new Example().child("First").child("Second").read, function(name){console.log(name)})
//now it should return the saved value
wrapper.wrap(new Example().child("First").child("Second").read, function(name){console.log(name)})
我想要實現此功能調用數據庫庫。當設備沒有互聯網連接時,它應該從保存的包裝狀態中讀取。
我實現這樣的包裝:
Wrapper = function() {
this.saved_funcs = {}
}
Wrapper.prototype.wrap = function (func, callbac) {
if (func in this.saved_funcs) {
callbac(this.saved_funcs[func]);
} else {
func(function (result) {
this.saved_funcs[func] = result;
callbac(result);
}.bind(this))
}
};
http://jsfiddle.net/boaLf6s3/1/
但是,當我執行的包裝則返回undefined?我的示例將始終存儲整個代碼以檢查方法是否已觸發。 這就是爲什麼我想問你如何實現這樣一個包裝?謝謝