-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomBind
More file actions
28 lines (20 loc) · 837 Bytes
/
customBind
File metadata and controls
28 lines (20 loc) · 837 Bytes
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
Function.prototype.bindCustom = function(thisObj, ...args) {
const boundFunc = (...argsInner) => this.call(thisObj, ...args, ...argsInner);
return boundFunc;
}
function add(a, b) {
return this.x + a + b;
}
console.log(add(1, 2)); // NaN
const boundAdd = add.bind({ x: 1 })
const customBoundAdd = add.bindCustom({ x: 1 })
console.log(boundAdd(1, 2)); // 4
console.log(customBoundAdd(1, 2)); // 4
console.log(boundAdd.call({ x: 2 }, 1, 2)); // still 4
console.log(customBoundAdd.call({ x: 2 }, 1, 2)); // still 4
console.log(boundAdd.bind({ x: 2 })(1, 2)); // still 4
console.log(customBoundAdd.bindCustom({ x: 2 })(1, 2)); // still 4
const boundAddWithArg = add.bind({ x: 1 }, 10)
const customBoundAddWithArg = add.bindCustom({ x: 1 }, 10)
console.log(boundAddWithArg(2)); // 13
console.log(customBoundAddWithArg(2)); // 13