js闭包例子

很经典的一个js闭包,收藏下

<script>

var user = "The window";

var box = {
	user:"The Box",
	getUser:function(){
		var that = this;
		return function(){
			return that.user;
		};
	}
}

//alert(box.getUser()());
//alert(box.getUser().call(box));
alert(box.getUser.call(window)());
</script>

猜你喜欢

转载自linhexiao.iteye.com/blog/2352662