-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.js
More file actions
53 lines (47 loc) · 1.79 KB
/
bind.js
File metadata and controls
53 lines (47 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* 如果 bind 得到的函数用作构造函数(new BindFunc),则 bind 不生效
* @param {*} context
*/
Function.prototype._bind = function (context, ...args) {
if (typeof this !== 'function') {
throw new TypeError(
'Function.prototype.bind called on incompatible target'
)
}
const self = this // 原函数
const fBound = function (...bindArgs) {
return self.apply(
// 当 fBound 作为构造函数时,this 指向实例,此时结果为 true,
// 将绑定函数的 this 指向该实例,可以让实例获得来自绑定函数的值。
this instanceof fBound ? this : context,
args.concat(bindArgs)
)
}
// 修改返回函数的 prototype 为绑定函数的 prototype,实例就可以继承绑定函数的原型中的值
// 不过,如果直接修改 fBound.prototype = this.prototype 会导致:
// 我们修改 fBound.prototype 时,this.prototype 也会被修改,所以用一个空函数中转
// let fNOP = function() {}
// fNOP.prototype = this.prototype
// fBound.prototype = new fNOP()
fBound.prototype = Object.create(this.prototype)
return fBound
}
/**
* 软绑定:绑定后 this 指向还可以被修改
*/
Function.prototype.softBind = function (context, ...args) {
if (typeof this !== 'function') {
throw new TypeError(
'Function.prototype.bind called on incompatible target'
)
}
const self = this
const fBound = function (...bindArgs) {
return self.apply(
!this || this === globalThis ? context : this,
args.concat(bindArgs)
)
}
fBound.prototype = Object.create(this.prototype)
return fBound
}