eval(字符串参数)
eval可以将传进来的字符串当成js表达式,在上下文环境中正常运行,前提是该字符串参数必须符合js语法规则;
eval一般用于将第三方API(字符串)转换为js可执行的实体(对象,数组等);
演示代码:
<script> //神奇的eval() /*eval(字符串参数) eval可以将传进来的字符串当成js表达式,在上下文环境中正常运行,前提是该字符串参数必须符合js语法规则; eval一般用于将第三方API(字符串)转换为js可执行的实体(对象,数组等);*/ var a = 10; var b =15; console.log(a+b); //25 console.log('a+b'); //1015 //把字符串当作表达式运行起来 console.log(alert(123)); //弹出窗口及输出undefined console.log(eval('alert(123)')); //弹出窗口及输出undefined console.log(eval('a+c')); //Uncaught ReferenceError: c is not defined </script>