forked from nhattruongniit/learn-javascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrow-function.js
154 lines (126 loc) · 2.88 KB
/
arrow-function.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// arrow function
// fat arrow
function sum(a, b) {
console.log(a + b)
}
var sum2 = function (a, b) {
console.log(a + b)
}
sum2(1, 2);
// arrow function
var sum3 = (a, b) => {
console.log(a + b)
}
sum3(5, 8);
var arr = [1, 2, 3];
const newArraFunc = arr.map(x => {
return x * x;
})
var newArr = arr.map(x => {
if(x === 2) {
return x * x
}
return x;
});
console.log('newArr: ', newArr);
/* === sự khác nhau function expression and arrow function */
console.log('sự khác nhau function expression and arrow function');
var func = {
name: 'aaa',
run: function() {
var run2 = function() {
console.log('aaa expression: ', this.name)
}
run2.bind(this)();
},
runArrow: function() {
var run2 = () => {
console.log('aaa arrow: ', this.name)
}
run2();
}
}
func.run();
func.runArrow();
var demo1 = {
foo: 'demo1'
}
var demo2 = {
foo: 'bar',
run: function() {
//cach 1
setTimeout(function() {
console.log('cach 1: ', this.foo)
}.bind(this), 2000)
//cach 2
setTimeout(function() {
console.log('cach 2: ', this.foo)
}.bind(demo2), 2000)
//cach 3
var that = this;
setTimeout(function() {
console.log('cach 3: ', that.foo)
}, 2000)
//cach 4
setTimeout(() => {
console.log('cach 4: ', this.foo)
}, 2000)
}
}
demo2.run();
// Sự khác nhau giữa Function thông thường và Arrow Function
// Function thông thường
let regularObj = function() {
this.name = 'An Vu';
return {
name: 'John',
getName: function() {
// this sẽ bị ghi đè vì this lúc này thuộc về Obj được trả về
return this.name;
}
};
}
console.log('regularObj: ' + regularObj().getName());
// Arrow Function
let arrowObj = function() {
this.name = 'An Vu';
return {
name: 'John',
getName: () => {
// this này không bị ghi đè bởi Obj mà vẫn giữ được this của hàm cha
return this.name;
}
};
}
console.log('arrowObj: ' + arrowObj().getName());
// Arrow Function thường được dùng trên Class
class Person {
constructor() {
this.name = 'An Vu';
this.age = 30;
const getName = () => {
console.log(`Name: ${this.name}`);
}
getName();
const getAge = function() {
// this ở function thường trong class sẽ là undefined
try {
console.log('Age: ' + this.age);
} catch (ex) {
console.log('This của Age: ' + typeof this);
}
}
getAge();
// Cách để sử dụng function thường bên trong
let getAgeBinding = function() {
// this ở function này đã bị ghi đè bởi this của class
console.log('Age: ' + this.age);
}
getAgeBinding = getAgeBinding.bind(this);
getAgeBinding();
}
getInfo() {
console.log(`Info: ${this.name} (${this.age})`);
}
}
let person = new Person().getInfo();