js 閉包

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
</html>

<script>
	var type={};

	for(var i=0,t;t=['String','Array','Object'][i++];){
		(function(t){
			type['is'+t] = function(obj){
				return Object.prototype.toString.call(obj)  == '[object '+ t+ ']'
			}
		})(t)
	}

	console.log(type.isArray([]));

	// 函數數組,若是不想經過索引來調用函數,能夠建立一個對象,如上所示代碼.
	var fun_arr=[];
	for(var i=0;i<5;i++){
		(function(i){
			fun_arr.push(function(args){
				console.log(args);
			});
		})(i)
	}

	console.log(fun_arr);
	// call
	fun_arr[0]([1,2,3]);
	fun_arr[1]("welcom javascrip");

    // 這樣作的目的是能夠將一些類型很接近的函數封裝在一個對象中,

</script>
相關文章
相關標籤/搜索