-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrowFunctions.js
More file actions
120 lines (92 loc) · 3.19 KB
/
ArrowFunctions.js
File metadata and controls
120 lines (92 loc) · 3.19 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
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
// Arrow Functions in JavaScript
// Arrow functions work very similarly to regular functions, but there are a couple of key differences.
// 1) Syntax differences
// Defining an arrow function with explicit return and block syntax
const addNumbers = (numberOne, numberTwo) => {
return numberOne + numberTwo
}
// Defining an arrow function with implicit return and parentheses
const addNumbers2 = (numberOne, numberTwo) => (
numberOne + numberTwo
)
console.log(addNumbers(1, 2))
console.log(addNumbers2(1, 2))
// Implicit returns can only include one expression
// Arrow function with a conditional and explicit return
const addNumbers3 = (numberOne, numberTwo) => {
if (numberOne > numberTwo) {
return numberOne + numberTwo
}
}
// Arrow function returning an object with explicit return
const returnAnObject = () => {
return {
name: 'Ruslan',
age: 26
}
}
// Arrow function returning an object with implicit return and parentheses
const returnAnObject2 = () => (
{
name: 'Ruslan',
age: 26
}
)
console.log(returnAnObject())
console.log(returnAnObject2())
// Arrow function with a single parameter and implicit return
const returnNumberPlusOne = numberOne => numberOne + 1
console.log(returnNumberPlusOne(3))
// Arrow function with multiple parameters and an array return
const returnNumbersPlusOne = (numberOne, numberTwo) => [numberOne + 1, numberTwo + 1]
console.log(returnNumbersPlusOne(3, 4))
// 2) Arrow functions do not bind a "this" keyword. "this" will be resolved lexically
const codingLanguage = {
name: 'JavaScript',
programmers: 17400000,
details: function () {
console.log(this) // "this" refers to the object
return `${this.name} has ${this.programmers} programmers`
}
}
console.log(codingLanguage.details())
// Returning a function within an object method
const codingLanguage2 = {
name: 'JavaScript',
programmers: 17400000,
details: function () {
console.log(this) // "this" refers to the object
return function () {
console.log(this) // "this" refers to the global object, not the object
return `${this.name} has ${this.programmers} programmers`
}
}
}
console.log(codingLanguage2.details()()) // Note: The inner function call returns undefined
// The arrow function will look up into its parent scope to find this.name and this.programmers
const codingLanguage3 = {
name: 'JavaScript',
programmers: 17400000,
details: function() {
return () => {
console.log(this) // "this" refers to the object
return `${this.name} has ${this.programmers} programmers`
}
}
}
console.log(codingLanguage3.details()())
// Common use cases
// Passing arrow functions as callbacks to array methods
const numbers = [1, 2, 3, 4, 5, 6, 7, 8]
const numbersTimesTen = numbers.map((number) => number * 10)
const numbersGreaterThanTwo = numbers.filter((number) => number > 2)
console.log(numbersTimesTen)
console.log(numbersGreaterThanTwo)
// Also common when handling events within React
function MyComponent() {
return (
<button onClick={() => console.log('click')}>
Click Me
</button>
)
}