- 帖子
- 766
- 积分
- 1451
- 技术
- 117
- 捐助
- 0
- 注册时间
- 2010-5-12
|
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. |
|