# bind-call-apply的区别
# bind
bind()最常用的方法是创建一个函数,使函数调用时拥有相同的this值
fun.bind(thisArg[, arg1[, arg2[, ...]]])
this.x = 1
const module1 = {
x: 2,
getX: function() {
return this.x
}
}
console.log(module1.getX()) // 2
let getValue = module1.getX
console.log(getValue()) // 1
let getTrueValue = module1.getX.bind(module1)
console.log(getTrueValue()) // 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# bind函数的兼容性写法
Function.prototype.bind = Function.prototype.bind || function(context){
var self = this;
return function(){
return self.apply(context, arguments);
};
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# call
call()方法调用函数,其具有指定的this和 分别提供的 参数
fun.call(thisArg, arg1, arg2...)
实现方法
Function.prototype.call2 = function(context, ...args) {
context = context || window;
let fn = Symbol()
context[fn] = this;
var result = context[fn](...args)
Reflect.deleteProperty(context, fn)
return result;
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# apply
apply()方法接收的是 一个包含多个参数的 数组
fun.apply(thisArg[, argsArray])
/* min/max number in an array */
var numbers = [5, 6, 2, 3, 7];
/* using Math.min/Math.max apply */
var max = Math.max.apply(null, numbers); /* This about equal to Math.max(numbers[0], ...) or Math.max(5, 6, ..) */
var min = Math.min.apply(null, numbers);
/* vs. simple loop based algorithm */
max = -Infinity, min = +Infinity;
for (var i = 0; i < numbers.length; i++) {
if (numbers[i] > max)
max = numbers[i];
if (numbers[i] < min)
min = numbers[i];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16