动态执行javascript代码,eval or Function,哪个性能好?
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>动态执行javascript代码</title> <script type="text/javascript"> String.prototype.code = function(){ return (new Function('with(this) { return ' + this + '}' )).call({}); }; var sCode = 'Math.round(3+2.5)*1.2'; var result = 0; for(var i=0;i<10000;i++){ result += sCode.code(); //result += eval(sCode); } alert(result); </script> </head> <body> </body> </html>