javascript当中eval用法
1)eval
例 4.1.1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
/*马克-to-win:var scriptCode = "c = a * b";
var a = 5;
var b = 10;
var c = 2;
eval(scriptCode);
以上的话就相当于:
eval("c = a * b");===c = a * b
eval是global的方法,
*/
var result = window.eval("1+2/4") ;//根据上面所说,result=1+2/4;
document.write(result)
var s="today=new Date();document.write(today.toLocaleString())"
eval(s)
//-->
</SCRIPT>
</BODY>
</HTML>
例 4.1.2
<HTML>
<HEAD>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<TITLE>在eclipse中直接open with火狐即可</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
//例1
var s = "2+31-18";
/*When the eval() function is called, it expects a string to be
passed to it as its single argument value. The contents of that
string should be syntactically correct executable script source
text.*/
document.write(eval(s));
var s1 = "2+31a-18";
/* note that we must comment out the following statement,
otherwise, it reports error.*/
//document.write(eval(s1));
//例2
eval("d =new Date();document.write(d.toLocaleString())")
//eval()函数的参数为字符串,功能是将该字符串执行出来。体会“执行”的意思!
//-->
</SCRIPT>
</BODY>
</HTML>
更多内容请见原文,文章转载自:https://blog.csdn.net/qq_43650923/article/details/102216676