-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbindTest.js
More file actions
67 lines (58 loc) · 1.86 KB
/
bindTest.js
File metadata and controls
67 lines (58 loc) · 1.86 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
54
55
56
57
58
59
60
61
62
63
64
65
66
Function.prototype.bind2 = function (oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable')
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {
},
fBound = function () {
// this instanceof fNOP === true时,说明返回的fBound被当做new的构造函数调用
return fToBind.apply(this instanceof fNOP
? this
: oThis,
// 获取调用时(fBound)的传参.bind 返回的函数入参往往是这么传递的
aArgs.concat(Array.prototype.slice.call(arguments)))
}
// 维护原型关系
if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype
}
// 下行的代码使fBound.prototype是fNOP的实例,因此
// 返回的fBound若作为new的构造函数,new生成的新对象作为this传入fBound,新对象的__proto__就是fNOP的实例
fBound.prototype = new fNOP()
console.log('==', fBound.prototype.constructor)
return fBound
}
Function.prototype.bind3 = function (oThis, ...args0) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable')
}
let fToBind = this
let fBound = function (...args1) {
if (new.target === fBound) {
return new fToBind(...args0.concat(args1))
}
return fToBind.apply(oThis, args0.concat(args1))
}
return fBound
}
function Foo () {
console.log(this)
}
new (Foo.bind2({}))()
//
// function foo1 () {
// this.count++
// }
// let obj = {
// count: 20
// }
// let newFoo = foo1.bind2(obj)
// newFoo()
// console.log(obj)