Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript设计模式之代理模式 #56

Open
pengjielee opened this issue Oct 31, 2019 · 0 comments
Open

Javascript设计模式之代理模式 #56

pengjielee opened this issue Oct 31, 2019 · 0 comments

Comments

@pengjielee
Copy link
Owner

代理模式是为一个对象提供一个代用品或占位符,以便控制对它的访问。

虚拟代理实现图片预加载

var myImage = (function(){
	var imgNode = document.createElement('img');
	document.body.appendChild(imgNode);

	return {
		setSrc: function(src){
			imgNode.src = src;
		}
	}
})();

var proxyImage = (function(src){
	var img = new Image();
	img.onload = function(){
		myImage.setSrc(this.src);
	}

	return {
		setSrc: function(src){
			myImage.setSrc('//loading.gif');
			img.src = src;
		}
	}
})();

proxyImage.setSrc('//real.jpg');

缓存代理计算乘积


var mult = function(){
	var a = 1;
	for(var i = 0; i < arguments.length; i++){
		a = a * arguments[i];
	}
	return a;
}

var proxyMult = function(){
	var cache = {};
	return function(){
		var args = Array.prototype.join.call(arguments,'',);
		if(args in cache){
			return cache[args];
		}
		return cache[args] = mult.apply(this,arguments)
	}
}

proxyMult(1,2,3,4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant