JS動態創建匿名函數及調用

<!DOCTYPE html>
<html>
<head>
	<title>JS動態創建匿名函數</title>
</head>
<body>
<script type="text/javascript">
	/**
	 * 動態生成function
	 * @return {[type]} [description]
	 */
	function createFunc(){
		return Function("console.log(this.name,'動態function');return 'success';");
	}

	/**
	 * 創建function
	 * @type {[type]}
	 */
	var newFunc = createFunc();
	console.log(newFunc);
	/**
	 * 執行function
	 * @type {String}
	 */
	var result = newFunc.call({
		name:"李四"
	});
	console.log(result);
</script>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章