Skip to content

Commit 0ca6603

Browse files
committed
Default Parameters and Hoisting Gotchas
1 parent c2910db commit 0ca6603

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

app.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
// Arrow Functions or Fat Arrow Functions
2-
// regular function - 'this' refers to parent, left of the dot
3-
// arrow function - 'this' refers to it's current surrounding scope
1+
// default parameters - a fallback to a value to prevent incase functionality breaks if the parameter is not passed i.e undefined. The argument/parameter passed will have more precedence than the default parameter
42

5-
const btn = document.querySelector(".btn");
3+
// arrow function gotchas - hositing doesn't work since it has variable name to defined
64

7-
btn.addEventListener("click", function () {
8-
console.log(this);
5+
sayHi();
96

10-
// Here, it points to window, since window is parent of setTimeOut
11-
// setTimeout(function () {
12-
// console.log(this);
13-
// this.style.color = "black";
14-
// }, 2000);
7+
const john = "John";
8+
const anna = "Anna";
159

16-
// Here, it points to surrounding scope i.e btn
17-
setTimeout(() => {
18-
console.log(this);
19-
this.style.color = "black";
20-
}, 2000);
21-
});
10+
function sayHi(person = "Peter") {
11+
console.log(`Hi ${person}`);
12+
}
13+
14+
const sayHello = (person = "Bob") => {
15+
console.log(`Hello ${person}`);
16+
};
17+
sayHello(anna);

0 commit comments

Comments
 (0)