Function
构造函数创建一个新的 Function
对象。直接调用此构造函数可用动态创建函数,但会遭遇来自 eval
的安全问题和相对较小的性能问题。然而,与 eval 不同的是,Function 构造函数只在全局作用域中运行。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
每个 JavaScript 函数实际上都是一个 Function
对象。运行 (function(){}).constructor === Function
便可以得到这个结论。
语法
new Function ([arg1[, arg2[, ...argN]],] functionBody)
参数
-
arg1, arg2, ... argN
-
被函数使用的参数的名称必须是合法命名的。参数名称是一个有效的JavaScript标识符的字符串,或者一个用逗号分隔的有效字符串的列表;例如“
×
”,“theValue
”,或“a,b
”。 -
functionBody
- 一个含有包括函数定义的 JavaScript 语句的 字符串。
描述
使用 Function
构造器生成的 Function
对象是在函数创建时解析的。这比你使用函数声明或者函数表达式并在你的代码中调用更为低效,因为使用后者创建的函数是跟其他代码一起解析的。
所有被传递到构造函数中的参数,都将被视为将被创建的函数的参数,并且是相同的标示符名称和传递顺序。
以调用函数的方式调用 Function
的构造函数(而不是使用 new
关键字) 跟以构造函数来调用是一样的。
属性和方法
全局的 Function
对象没有自己的属性和方法,但是,因为它本身也是一个函数,所以它也会通过原型链从自己的原型链 Function.prototype
上继承一些属性和方法。
原型对象
属性
-
Function.arguments
-
以数组形式获取传入函数的所有参数。此属性已被
arguments
替代。 -
-
用于指定的函数的参数的个数,但已被删除。使用length
属性代替。 -
Function.caller
- 获取调用函数的具体对象。
-
Function.length
- 获取函数的接收参数个数。
-
Function.name
- 获取函数的名称。
-
Function.displayName
- 获取函数的display name。
-
Function.prototype.constructor
-
声明函数的原型构造方法,详细请参考
Object.constructor
。
方法
-
Function.prototype.apply()
- 在一个对象的上下文中应用另一个对象的方法;参数能够以数组形式传入。
-
Function.prototype.bind()
- bind()方法会创建一个新函数,称为绑定函数.当调用这个绑定函数时,绑定函数会以创建它时传入 bind()方法的第一个参数作为 this,传入 bind()方法的第二个以及以后的参数加上绑定函数运行时本身的参数按照顺序作为原函数的参数来调用原函数.
-
Function.prototype.call()
- 在一个对象的上下文中应用另一个对象的方法;参数能够以列表形式传入。
-
Function.prototype.isGenerator()
-
若函数对象为
generator,返回true,反之返回false
。 -
Function.prototype.toSource()
-
获取函数的实现源码的字符串。 覆盖了
Object.prototype.toSource
方法。 -
Function.prototype.toString()
-
获取函数的实现源码的字符串。覆盖了
Object.prototype.toString
方法。
实例
Function
实例从 Function.prototype
继承了一些属性和方法。 同其他构造函数一样, 你可以改变构造函数的原型从而使得所有的 Function
实例的属性和方法发生改变。
示例
传入参数调用 Function 构造函数
下面的代码会创建一个需要两个参数的 Function
对象。
// 可以直接在 JavaScript 控制台中运行 // 创建了一个能返回两个参数和的函数 const adder = new Function("a", "b", "return a + b"); // 调用函数 adder(2, 6); // > 8
参数 "a
" 和 "b
" 是参数的名字,在函数体中被使用,"return a + b
"。
Function 构造器与函数声明之间的不同
Functions created with the Function
constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function
constructor was created. This is different from using eval
with code for a function expression.
var x = 10; function createFunction1() { var x = 20; return new Function('return x;'); // 这里的 x 指向最上面全局作用域内的 x } function createFunction2() { var x = 20; function f() { return x; // 这里的 x 指向上方本地作用域内的 x } return f; } var f1 = createFunction1(); console.log(f1()); // 10 var f2 = createFunction2(); console.log(f2()); // 20
虽然这段代码可以在浏览器中正常运行,但在 Node.js 中,f1()
会产生一个 ReferenceError
,因为找不到变量 x
。这是因为,在 Node 中,顶级作用域不是全局作用域,而 x
其实是在模块的作用域之中。
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262) Function |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Function |
Standard | |
ECMAScript Latest Draft (ECMA-262) Function |
Draft |
浏览器兼容性
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Function |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
apply |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
arguments
|
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
arity
|
Chrome No support No | Edge No support No | Firefox No support No | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android No support No | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
bind |
Chrome Full support 7 | Edge Full support 12 | Firefox Full support 4 | IE Full support 9 | Opera Full support 11.6 | Safari Full support 5.1 | WebView Android Full support 4 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 12 | Safari iOS Full support 6 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
call |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
caller
|
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 8 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
displayName
|
Chrome No support No | Edge No support No | Firefox Full support 13 | IE No support No | Opera No support No | Safari ? | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 14 | Opera Android No support No | Safari iOS ? | Samsung Internet Android No support No | nodejs No support No |
isGenerator
|
Chrome No support No | Edge No support No | Firefox No support 5 — 58 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android No support 5 — 58 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
length |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
name |
Chrome Full support 15 | Edge Full support 14 | Firefox Full support 1 | IE No support No | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
prototype |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toSource
|
Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
toString |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.