-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.js
42 lines (32 loc) · 782 Bytes
/
bind.js
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
// const myModule = {
// x: 42,
// getX: function() {
// return this.x;
// }
// }
// const module2 = {
// x: 20,
// getX: function() {
// return this.x;
// }
// }
// console.log(myModule.getX());
// const unboundGetX = myModule.getX();
// console.log(unboundGetX); // The function gets invoked at the global scope
// const boundGetX = unboundGetX.bind(module2);
// console.log(boundGetX);
const toto = {
x: 42,
getX: function() {
return this.x;
}
}
const module2 = {
x: 20,
}
const unboundGetX = toto.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined
const boundGetX = unboundGetX.bind(module2);
console.log(boundGetX());
// expected output: 20