[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
function alert(){return echo.apply({},arguments);}
var print = new Function("return echo.apply(this,arguments);");
这两种定义函数的方式有什么区别?
powerbat 发表于 2011-5-21 15:58

没有实质区别,但是不建议用new Function,因为效率会稍微低一点。

详见《Professional JavaScript for Web Developers 2nd Edition》,Chapter 5
The last way to define functions is by using the  Function  constructor, which accepts any number of arguments. The last argument is always considered to be the function body, and the previous arguments enumerate the new function's arguments. Take this for example:

var sum = new Function("num1", "num2", "return num1 + num2");   //not recommended

Technically this is a function expression. This syntax is not recommended because it causes a double interpretation of the code (once for the regular ECMAScript code and once for the strings that are passed into the constructor), and thus can affect performance. However, it's important to think of functions as objects, and function names as pointers  —  this syntax is great at representing that concept.

TOP

返回列表