函数的构造函数
是Function
,函数
是Function
的实例。
在JavaScript中使用Function
能够实例化函数对象,也是说在JavaScript中函数与普通对象同样,也是一个对象类型,函数是JavaScript的一等公民。函数
语法:code
new Function(arg0, arg1, arg2, arg3, argN, body);
Function
中的参数所有是字符串该构造函数的做用是将参数连接起来组成函数对象
function foo() { console.log("foo"); } foo(); /* 等价于 */ var func = new Function("console.log(\"foo\")"); func();
function foo(num, desc) { console.log(num,desc); } foo(123,"我是描述"); /* 等价于 */ var func = new Function("num", "des","console.log(num,des)"); func(123, "我是描述");
注意,使用Function
定义函数时,若是有参数的话,参数的名字必定要和函数体中的参数名字保持一致,如:num
对应num
;desc
对应desc
。ip