<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>无标题文档</title>
<script>
//动态执行 javascript 函数(带参数):
function g1(i){return 21+i;}
function g2(i){return 22+i;}
function g3(i){return 23+i;}
function g4(i){return 24+i;}
function g(a,n){return a(n)};
function f(i,j) { return g(eval("g"+i),j); }
document.write(f(1,1)+";"); //22
document.write(f(2,2)+";"); //24
document.write(f(3,3)+";"); //26
document.write(f(4,4)+";"); //28
</script>
</head>
<body>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>无标题文档</title>
<script>
//动态执行 javascript 函数(带参数):
function g1(i){return 21+i;}
function g2(i){return 22+i;}
function g3(i){return 23+i;}
function g4(i){return 24+i;}
function g(a,n){return a(n)};
function f(i,j) { return g(eval("g"+i),j); }
document.write(f(1,1)+";"); //22
document.write(f(2,2)+";"); //24
document.write(f(3,3)+";"); //26
document.write(f(4,4)+";"); //28
</script>
</head>
<body>
</body>
</html>
本文通过一个具体的HTML页面示例介绍了如何在JavaScript中实现动态函数调用,并展示了如何利用eval函数配合字符串来实现这一功能。这种方法可以使得代码更加灵活,但也可能带来一些安全隐患。
1448

被折叠的 条评论
为什么被折叠?



