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>

 

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