Repository navigation
Expand file tree
/
Copy pathFunctions.js
More file actions
49 lines (40 loc) · 1.46 KB
/
Copy pathFunctions.js
File metadata and controls
49 lines (40 loc) · 1.46 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
// Variables
let x, y, z;
x = 5;
y = 10;
z = 15;
// Normal function (requires 'function' keyword and 'return')
function normalFunc(i, j) {
return i + j;
}
// Arrow function (implicit return for single expression)
arrowFunc = (i, j) => i + j;
// Arrow function without implicit return (needs curly braces)
arrowFuncNonImplicit = (i, j) => { return i + j; };
let oldResult = normalFunc(1, 2); // 3
let result = arrowFunc(1, 2); // 3
let nonRes = arrowFuncNonImplicit(1, 2); // 3
console.log(oldResult, result, nonRes);
// "This" example: Arrow function inherits "this"
function TimerArrow() {
this.seconds = 0;
setInterval(() => {
this.seconds++; // 'this' refers to the TimerArrow object
console.log(this.seconds);
}, 1000);
}
const timerArrow = new TimerArrow();
// "This" example: Normal function does not inherit "this"
function TimerNormal() {
this.seconds = 0;
setInterval(function() {
this.seconds++; // 'this' refers to the global object (or undefined in strict mode)
console.log(this.seconds);
}, 1000);
}
const timerNormal = new TimerNormal();
// Arguments example
const arrowFuncWithRest = (...args) => args.reduce((sum, num) => sum + num, 0); // No 'arguments' object
const normalFuncWithArguments = function() { return Array.from(arguments).reduce((sum, num) => sum + num, 0); }; // 'arguments' object
console.log(arrowFuncWithRest(1, 2, 3)); // 6
console.log(normalFuncWithArguments(1, 2, 3)); // 6