-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrow-function.js
185 lines (135 loc) · 3.47 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Expression bodies
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
var pairs = evens.map(v => ({even: v, odd: v + 1}));
// Statement bodies
nums.forEach(v => {
if (v % 5 === 0)
fives.push(v);
});
// Lexical this
var bob = {
_name: "Bob",
_friends: [],
printFriends() {
this._friends.forEach(f =>
console.log(this._name + " knows " + f));
}
}
/////////////////////
var Message = (text) => {
this.text = text;
};
// Throws "TypeError: Message is not a constructor"
var helloMessage = new Message('Hello World!');
////////////////////
let callback;
callback = callback || function() {}; // ok
callback = callback || () => {}; // SyntaxError: invalid arrow-function.js arguments
callback = callback || (() => {}); // ok
///////////////////
setTimeout( _ => {
console.log("I happen sooner");
setTimeout( _ => {
// deeper code
console.log("I happen later");
}, 1);
}, 1);
////////////////////
var arguments = 42;
var arr = () => arguments;
arr(); // 42
function foo() {
var f = () => arguments[0]; // foo's implicit arguments binding
return f(2);
}
foo(1); // 1
///////////////////////
function foo() {
var f = (...args) => args[0];
return f(2);
}
foo(1); // 2
////////////////////////
function Timer() {
this.s1 = 0;
this.s2 = 0;
setInterval(() => this.s1++, 1000);
setInterval(function () {
this.s2++;
}, 1000);
}
var timer = new Timer();
setTimeout(() => console.log('s1: ', timer.s1), 3100);
setTimeout(() => console.log('s2: ', timer.s2), 3100);
// s1: 3
// s2: 0
///////////////////////
'use strict';
function Timer() {
var _this = this;
this.s1 = 0;
this.s2 = 0;
setInterval(function () {
return _this.s1++;
}, 1000);
setInterval(function () {
this.s2++;
}, 1000);
}
/////////////////////
var adder = {
base : 1,
add : function(a) {
var f = v => v + this.base;
return f(a);
},
addThruCall: function(a) {
var f = v => v + this.base;
var b = {
base : 2
};
return f.call(b, a);
}
};
console.log(adder.add(1)); // 2
console.log(adder.addThruCall(1)); // 2
///////////////////////
function MyCat(name) {
this.catName = name;
}
MyCat.prototype.sayCatName = () => {
console.log(this === window); // => true
return this.catName;
};
var cat = new MyCat('Mew');
cat.sayCatName(); // => undefined
/////////////////////////////////
var calculate = {
array: [1, 2, 3],
sum: () => {
console.log(this === window); // => true
return this.array.reduce((result, item) => result + item);
}
};
console.log(this === window); // => true
// Throws "TypeError: Cannot read property 'reduce' of undefined"
calculate.sum();
/////////////////////////
document.addEventListener('click', () => {
this.innerHTML = '123';
});
var init = function() {
document.addEventListener('click',
event => this.innerHTML = '123', false);
};
///////////////////////
document.addEventListener('click', function () {
undefined.innerHTML = '123';
});
var init = function init() {
var _this = this;
document.addEventListener('click', function (event) {
return _this.innerHTML = '123';
}, false);
};