Function
Function
构造函数 创建一个新的Function对象
每个函数实际上都是一个Function
对象。
语法
new Function ([arg1[, arg2[, ...argN]],] functionBody)
参数
arg1, arg2, ... argN被函数使用的参数的名称必须是合法命名的。参数名称是一个有效的JavaScript标识符的字符串,或者一个用逗号分隔的有效字符串的列表;例如“×”,“theValue”,或“A,B”。functionBody一个含有包括函数定义的JavaScript语句的字符串。
描述
所有被传递到构造函数中的参数,都将被视为将被创建的函数的参数,并且是相同的标示符名称和传递顺序。
以调用函数的方式调用Function的构造函数 (不是用new关键字) 跟以构造函数来调用是一样的.
Function
的属性和方法
全局的Function对象没有自己的属性和方法, 但是, 因为它本身也是函数,所以它也会通过原型链从Function.prototype
上继承部分属性和方法。
Function
的原型对象
属性
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
实例从Function.prototype
继承了一些属性和方法。 同其他构造函数一样, 你可以改变构造函数的原型从而使得所有的Function实例的属性和方法发生改变。
示例
传入参数调用Function
构造函数
下面的代码会创建一个需要两个参数的Function
对象
// Example can be run directly in your JavaScript console
// Create a function that takes two arguments and returns the sum of those arguments
var adder = new Function('a', 'b', 'return a + b');
// Call the function
adder(2, 6);
// > 8
参数"a
"和"b
"是参数的名字,在函数体中被使用,"return a + b
"。
函数构造器和函数声明的区别
使用Function
构造器创建的函数不会为其创建上下文及闭包; 他们总是在全球范围内创建。在运行它们时,它们只能访问自己的局部变量和全局变量,而不能调用Function
构造函数的范围。这与用eval
函数表达式的代码不同。
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
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262)The definition of 'Function' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Function' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262)The definition of 'Function' in that specification. | Living Standard | |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com