Function

每个 JavaScript 函数实际上都是一个 Function 对象。运行 (function(){}).constructor === Function // true 便可以得到这个结论。

构造函数

Function 构造函数创建一个新的 Function 对象。直接调用此构造函数可用动态创建函数,但会遇到和 eval 类似的的安全问题和(相对较小的)性能问题。然而,与 eval 不同的是,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替代。
Function.arity
用于指定的函数的参数的个数,但已被删除。使用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 构造器与函数声明之间的不同

由 Function 构造器创建的函数不会创建当前环境的闭包,它们总是被创建于全局环境,因此在运行时它们只能访问全局变量和自己的局部变量,不能访问它们被 Function 构造器创建时所在的作用域的变量。这一点与使用 eval 执行创建函数的代码不同。

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() 会产生一个“找不到变量 x ”的 ReferenceError。这是因为在 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 (ECMA-262)
Function
Living Standard

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
FunctionChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
Function() constructorChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
applyChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 4Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
arguments
DeprecatedNon-standard
Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
bindChrome Full support 7Edge Full support 12Firefox Full support 4IE Full support 9Opera Full support 11.6Safari Full support 5.1WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 12Safari iOS Full support 6Samsung Internet Android Full support 1.0nodejs Full support Yes
callChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 4Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
caller
Non-standard
Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 8Opera Full support 9.6Safari Full support 3WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
displayName
Non-standard
Chrome No support NoEdge No support NoFirefox Full support 13IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 14Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
lengthChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
nameChrome Full support 15Edge Full support 14Firefox Full support 1IE No support NoOpera Full support 10.5Safari Full support 6WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 6Samsung Internet Android Full support 1.0nodejs Full support Yes
toSource
Non-standard
Chrome No support NoEdge No support NoFirefox No support 1 — 74
Notes
No support 1 — 74
Notes
Notes Starting in Firefox 74, toSource() is no longer available for use by web content. It is still allowed for internal and privileged code.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 4Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
toStringChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes

Legend

Full support  
Full support
No support  
No support
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.
See implementation notes.
See implementation notes.

相关链接