每个 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
]]])
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 定义。 |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
|
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 |
完整支持
不支持
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
function
statement
function
expression
function*
statement
function*
expression
AsyncFunction
GeneratorFunction
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()