每个 JavaScript 函数实际上都是 Function 对象。可以见到这通过代码 (function(){}).constructor === Function ,其返回 true。

构造函数

Function()
创建新的 Function 对象。直接调用构造函数可以动态创建函数,但会遭遇安全性和类似性 (但远没有那么重要) 的性能问题对于 eval 。不管怎样,不像 eval, Function 构造函数创建只在全局作用域内执行的函数。

实例特性

Function.arguments
数组对应传递给函数的自变量。
这被弃用作为特性在 Function 。使用 arguments 对象 (函数内可用) 代替。
Function.caller
指定援引目前正执行函数的函数。
此特性被弃用,且只对一些非严格函数起作用。
Function.displayName

函数的显示名称。

Function.length

指定函数期望的自变量数。

Function.name

函数的名称。

实例方法

Function.prototype.apply( thisArg [, argsArray ])
调用函数并设置其 this 以提供 thisArg 。自变量可以传递作为 Array 对象。
Function.prototype.bind( thisArg [, arg1 [, arg2 [, ... argN ]]])
Creates a new function which, when called, has its this set to the provided thisArg . Optionally, a given sequence of arguments will be prepended to arguments provided the newly-bound function is called.
Function.prototype.call( thisArg [, arg1 , arg2 , ... argN ])
调用函数并设置其 this 以提供值。可以按原样传递自变量。
Function.prototype.toString()
返回表示函数源代码的字符串。
覆写 Object.prototype.toString 方法。

范例

函数构造函数和函数声明之间的区别

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;'); // this |x| refers global |x|
}
function createFunction2() {
    var x = 20;
    function f() {
        return x; // this |x| refers local |x| above
    }
    return f;
}
var f1 = createFunction1();
console.log(f1());          // 10
var f2 = createFunction2();
console.log(f2());          // 20
					

While this code works in web browsers, f1() 将产生 ReferenceError 在 Node.js,因为 x 找不到。这是因为 Node 顶级作用域不是全局作用域,且 x will be local to the module.

规范

规范
ECMAScript (ECMA-262)
在该规范中的 Function 定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Function Chrome 1 Edge 12 Firefox 1 IE 4 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes
Function() 构造函数 Chrome 1 Edge 12 Firefox 1 IE 4 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes
apply Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 4 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes
arguments 弃用 非标 Chrome 1 Edge 12 Firefox 1 IE 4 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes
bind Chrome 7 Edge 12 Firefox 4 IE 9 Opera 11.6 Safari 5.1 WebView Android 4 Chrome Android 18 Firefox Android 4 Opera Android 12 Safari iOS 6 Samsung Internet Android 1.0 nodejs Yes
call Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 4 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes
caller 非标 Chrome 1 Edge 12 Firefox 1 IE 8 Opera 9.6 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes
displayName 非标 Chrome No Edge No Firefox 13 IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 14 Opera Android No Safari iOS No Samsung Internet Android No nodejs No
length Chrome 1 Edge 12 Firefox 1 IE 4 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes
name Chrome 15 Edge 14 Firefox 1 IE No Opera 10.5 Safari 6 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 6 Samsung Internet Android 1.0 nodejs Yes
toSource 非标 Chrome No Edge No Firefox 1 — 74
1 — 74
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 Opera No Safari No WebView Android No Chrome Android No Firefox Android 4 Opera Android No Safari iOS No Samsung Internet Android No nodejs No
toString Chrome 1 Edge 12 Firefox 1 IE 5 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes

图例

完整支持

完整支持

不支持

不支持

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

另请参阅

元数据

  • 最后修改: