-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbind.js
64 lines (46 loc) · 912 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// console.log(this);
function Car() {
this.name = 'Car';
this.position = 0;
this.counter = 0;
}
// Car.prototype.drive = async function (distance) {
// this.position += distance;
//
// console.log("THIS 1", this);
//
// return new Promise(resolve=> {
// setTimeout(function () {
//
// console.log("THIS 2", this);
//
// this.counter ++;
//
// console.log("NAN", this.counter);
//
// resolve();
//
//
//
// }.bind(this), 1000)
// })
//
// }
Car.prototype.drive = async function (distance) {
this.position += distance;
console.log("THIS 1", this);
return new Promise(resolve=> {
setTimeout(() => {
console.log("THIS 2", this);
this.counter ++;
console.log("NAN", this.counter);
resolve();
}, 1000)
})
}
const car = new Car();
(async () => {
console.log("X");
await car.drive(20);
console.log(car);
})()