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>
发布了170 篇原创文章 · 获赞 30 · 访问量 61万+

猜你喜欢

转载自blog.csdn.net/u010989191/article/details/105368029
今日推荐