保存方法结果的包装器

Wrapper to save methods results

本文关键字:包装 结果 方法 保存      更新时间:2023-09-26

我正试图为一个链式方法编写一个包装器:

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>