# Function
JavaScript 的每一个函数都是一个 Function 对象。
(() => {}).constructor === Function; // true
1
# 构造函数
可以通过new Function()
创建一个函数。
示例
const func = new Function("a", "b", "return a + b");
func(1, 2); // 3
1
2
2
提示
一般使用字面量来直接创建函数,例如const func = () => {}
# 属性和方法
Function 没有自己属性和方法。
# Function 原型对象
# Function.prototype.length
获取函数接受参数个数。
# Function.prototype.constructor
构造函数指向 Funciton 本身。
# Function.prototype.apply(thisArg[, argsArray])
在确定 this 值和若干调用参数的情况下调用函数。
示例
const cat = {
name: "cat",
};
const dog = {
name: "dog",
};
const getName = function() {
console.log(this.name);
};
getName.apply(cat); // cat
getName.apply(dog); // dog
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# Function.prototype.call(thisArg[, arg1][, arg2])
方法功能和Function.prototype.apply()
一样, 只是参数不同。
# Function.prototype.bind(thisArg)
创建一个新的函数,并返回。新函数的执行上下文,也就是this
会指向bind()
的第一个参数。
示例
const module = {
x: 42,
getX: function() {
return this.x;
},
};
const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined
const boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# Function.prototype.toString()
覆盖了Object.prototype.toString()
方法,获取函数的实现源码的字符串。