-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrowFunctionBasics.html
110 lines (68 loc) · 2.65 KB
/
arrowFunctionBasics.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Arrow Function Basics</title>
</head>
<body>
<p href="https://javascript.info/arrow-functions-basics">Arrow Function Basics</p>
<p href="https://javascript.info/arrow-functions">Follow up page</p>
<script>
/*
// simple concise syntax for creating functions, often better
// than FEs.
let func = (arg1, arg2, ...a, argN) => expression;
// a shorthand way of writing
let func = function(arg1, arg2, ...a, argN) {
return expression;
};
*/
/*
// Full example
let sum = (a, b) => a + b;
// shorthand for
let sum = function(a, b) {
return a + b;
};
alert(sum (1, 2) ); // 3
*/
// parenthesis can be omitted if a single argument
// let double = n => n*2; == let double = function(n) { return n*2 };
// If NO argument, parentheses will be empty but should still be in place
// let sayHi = () => alert('hello!');
// arrows can dynamically create functions as well
/*
let age = prompt('How old are you?', 18);
let welcome = (age < 18) ?
() => alert('Hello!') :
() => alert('Greetings!');
welcome();
*/
// without curly braces (...args) => expression
// right side is expression, function evals and returns result
// With curly braces (...args) => {body}
// brackets allow multiples statements inside the function
// !! an explicit 'return' is required to return something.
// Tasks
//oof.
/*
let question = ('do you concur?');
let ask = (question);
let welcome = (ask, yes, no);
() => confirm(question) (yes(), alert('You agreed'));
() => no() (alert("you cannceled."));
*/
// Solution
// the answer was in the question replace FUNCTION EXPRESSIONS with arrow func
function ask(question, yes, no) {
if (confirm(question)) yes();
else no()
}
ask (
'Do you concur?',
() => alert('you agreed'),
() => alert('you don\'t agree?')
)
</script>
</body>
</html>