-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrow-revisited.js
More file actions
79 lines (57 loc) 路 1.91 KB
/
Copy pathArrow-revisited.js
File metadata and controls
79 lines (57 loc) 路 1.91 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
67
68
69
70
71
72
73
74
75
76
77
78
79
//: Arrow function revisited
//* Arrow function have no "this"
/* arrow function do not have this. If this is accessed it is taken from the outside. */
let group = {
title: "Our Group",
students: ["Jhon", "Peter", "Ankit", "Alice"],
showList() {
this.students.forEach((student) =>
console.log(this.title + ": " + student)
);
},
};
group.showList();
/* Here is forEach, the arrow function is used, so this.title in it is exactly the same as in the outer method showList. That is: group.title */
//! If we used a "regular" function there would be an error
let students = {
title: "Our Group",
students: ["Jhon", "Peter", "Ankit", "Alice"],
showList() {
this.students.forEach(function (student) {
// Error: Cannot read property "title" of undefined
console.log(this.title + ": " + student);
});
},
};
students.showList();
//! The error because forEach runs function with this=undefined by default, so the attempt to access undefined.title is made
//? Arrow function can't run with new
/* Not having this naturally means another limitation: arrow function can't be used as constructors. They can't be called with new */
//* Arrow have no "arguments"
/* Arrow functions also have no arguments variable.
=> That's great for decorators, when we need to forward a call with the current this and arguments. */
function defer(f, ms) {
return function () {
setTimeout(() => f.apply(this, arguments), ms);
};
}
function sayHi(who) {
console.log(`Hello ${who}`);
}
let sayHiDeferred = defer(sayHi, 2000);
sayHiDeferred("Jhon"); // Hello, Jhon after 2 second
// The same without an arrow function would look like:
function defer2(f, ms) {
return (...args) => {
let ctx = this;
setTimeout(function () {
return f.apply(ctx, args);
}, ms);
};
}
/*
=> DO NOT HAVE -> this
=> DO NOT HAVE -> arguments
=> CAN'T BE CALLED WITH -> new
=> THEY ALSO DON'T HAVE -> super
*/