保存方法结果的包装器
Wrapper to save methods results
我正试图为一个链式方法编写一个包装器:
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/
但是当我执行包装器时,它返回未定义的?我的例子总是存储整个代码,以检查该方法是否已经被触发 这就是为什么我想问你,你将如何实现这样一个包装器 感谢
您试图引用的函数需要从对象的上下文中执行,因为它使用this
。如果您只传递对象并调用read
(而不是),效果会更好
Example = function(){
this.text = "";
}
Example.prototype.child = function(name){
this.text += name + " " + name;
return this;
}
Example.prototype.read = function(callbac){
callbac(this.text);
}
Wrapper = function(){
this.saved_funcs = {}
}
Wrapper.prototype.wrap = function(obj, callbac){
if(obj.read in this.saved_funcs){
callbac(this.saved_funcs[obj.read]);
}else{
obj.read(function(result){
this.saved_funcs[obj.read] = result;
callbac(result);
}.bind(this))
}
};
new Example().child("First").child("Second").read(function(name){document.getElementById("normal").innerHTML = name})
wrapper = new Wrapper();
wrapper.wrap(new Example().child("First").child("Second"), function(name){console.log(name)})
wrapper.wrap(new Example().child("First").child("Second"), function(name){ document.getElementById("saved").innerHTML = name })
<h2>
Normal Result
</h2>
<p id="normal">
</p>
<h2>
Saved Result
</h2>
<p id="saved">
</p>
或者你可以像这样绑定你的函数:
Example = function(){
this.text = "";
}
Example.prototype.child = function(name){
this.text += name + " " + name;
return this;
}
Example.prototype.read = function(callbac){
callbac(this.text);
}
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))
}
};
new Example().child("First").child("Second").read(function(name){document.getElementById("normal").innerHTML = name})
wrapper = new Wrapper();
var child = new Example().child('First').child('Second');
var read = child.read.bind(child);
wrapper.wrap(read, function(name){console.log(name)})
wrapper.wrap(read, function(name){ document.getElementById("saved").innerHTML = name })
<h2>
Normal Result
</h2>
<p id="normal">
</p>
<h2>
Saved Result
</h2>
<p id="saved">
</p>
相关文章:
- 奇怪的Javascript结果
- 如何将函数包装在函数中以避免代码重复
- Javascript(jQuery)给了我奇怪的结果
- 同位素库错误:未捕获错误无布局模式包装生产线8
- JSONP请求返回结果,但也触发error_callback
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 为什么要包装每一个原型“;类“;JS中具有匿名函数的对象
- Javascript,输出结果后页面不断刷新
- 这是使用html快照和谷歌获取的预期结果吗?SEO/SPA
- 将地理编码结果转换为php变量以发布到mysql数据库
- AngularJS/HTML/Bootstrap元素用于动态搜索结果
- JQuery:向多个匹配结果添加换行符的最简单方法
- 在'物品包装'
- 当查询不在displayField中时,引导Ajax Typeahead不显示结果
- webpack代码拆分了handlerbs文件——结果是文件很大
- JavaScript循环无法正确计算/显示结果
- 用
标记包装 JQuery 函数结果
- 保存方法结果的包装器
- 如何从包装函数返回javascript ajax调用的结果
- Restangular -如何获得请求的普通结果(没有包装)