Skip to content

Commit 71ea4de

Browse files
committed
complete js behind the scenes
1 parent 1dd329d commit 71ea4de

File tree

2 files changed

+166
-2
lines changed

2 files changed

+166
-2
lines changed

08-behind-the-scenes/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,63 @@ f(); // undefined
271271
```
272272

273273
We get undefined because `f` is not attached to any object. It is just an ordinary function and since the `this` keyword is undefined in an regular function, the result of `f` will too be undefined.
274+
275+
# Regular Functions vs Arrow Functions
276+
277+
There are a few pitfalls related to using the `this` keyword in regular functions and arrow functions that require mentioning.
278+
279+
```js
280+
const eke = {
281+
firstName: "Victor",
282+
year: 1997,
283+
calcAge: function () {
284+
console.log(2022 - this.year);
285+
console.log(this);
286+
},
287+
288+
greet: () => console.log(`Hey, ${this.firstName}`),
289+
};
290+
291+
eke.greet();
292+
```
293+
294+
In the code example above, the result of calling the greet method —which is an arrow function is undefined. Because remember an arrow functions does not get it's own `this` keyword, it will then use the `this` keyword from the global scope. In this scenario, `firstName` is not in the global scope therefore, the result will be `undefined`.
295+
296+
This can be quite dangerous in a situation `var` is used to create variables because variables declared with `var` creates properties on the global object.
297+
298+
```js
299+
var firstName = "Matilda"; // Result of eke.greet will now be Hey, matilda
300+
```
301+
302+
The reason this happens is because `this` keyword points to the window object in the `greet` method, and since `firstName` was declared as Matilda using the `var` keyword, `this.firstName` will now produce Hey, Matilda.
303+
304+
This is another good reason to not use the `var` keyword to declare a variable. From this example, the big takeaway is to not use an arrow function as a method but normal functions. Just following this rule will prevent the bug we illustrated in the example above.
305+
306+
Another final example of one of the pitfalls of using the `this` keyword is when we have a function inside a method.
307+
308+
# Primitive vs Objects (Primitive vs Reference Types)
309+
310+
Primitive values inclues:
311+
312+
- Number
313+
- String
314+
- Boolean
315+
- Undefined
316+
- Null
317+
- Symbol
318+
- BigInt
319+
320+
Objects
321+
Everyting else are basically objects
322+
323+
- Object literal
324+
- Arrays
325+
- Functions
326+
and many more..
327+
328+
For the sake of memory management, we refer to primitive as primitive types and objects as refrence types.
329+
Primitive types are stored in the `Call stack` while refrence types are stored in the `Heap`.
330+
331+
It's a misconceptionn that all variables declared with the `const` keyword are immutable. However this is only true for primitive values but not for refrence values.
332+
333+
# Primitive vs Objects in Practice

08-behind-the-scenes/script.js

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
2-
2+
/*
33
console.log(this);
44
55
const calcAge = function (birthYear) {
@@ -31,4 +31,108 @@ matilda.calcAge = eke.calcAge;
3131
matilda.calcAge();
3232
3333
const f = eke.calcAge;
34-
// f();
34+
f() */ // Regular functions vs Arrow functions
35+
var firstName = "Matilda";
36+
37+
/* const eke = {
38+
firstName: "Victor",
39+
year: 1997,
40+
calcAge: function () {
41+
// console.log(this);
42+
console.log(2022 - this.year);
43+
44+
// Solution 1
45+
// const self = this; // self or that
46+
// const isMillenial = function () {
47+
// console.log(self);
48+
// // console.log(this.year >= 1981 && this.year <= 1996);
49+
// console.log(self.year >= 1981 && self.year <= 1996);
50+
// };
51+
// isMillenial();
52+
53+
// Solution 2
54+
const isMillenial = () => {
55+
console.log(self);
56+
// console.log(this.year >= 1981 && this.year <= 1996);
57+
console.log(self.year >= 1981 && self.year <= 1996);
58+
};
59+
isMillenial();
60+
},
61+
62+
greet: () => console.log(`Hey ${this.firstName}`),
63+
};
64+
65+
eke.greet();
66+
eke.calcAge();
67+
68+
// Arguments keyword
69+
const addExpr = function (a, b) {
70+
console.log(arguments);
71+
return a + b;
72+
};
73+
74+
addExpr(2, 5);
75+
addExpr(2, 5, 8, 12);
76+
77+
var addArrow = (a, b) => {
78+
console.log(arguments);
79+
return a + b;
80+
};
81+
addArrow(2, 4, 6);
82+
*/
83+
84+
// Primitives vs Objects
85+
86+
let age = 30;
87+
let oldAge = age;
88+
age = 31;
89+
console.log(age);
90+
console.log(oldAge);
91+
92+
const me = {
93+
name: "Victor",
94+
age: 30,
95+
};
96+
97+
const friend = me;
98+
friend.age = 27;
99+
console.log(friend);
100+
console.log("Me", me);
101+
102+
// Primitive Types - Call Stack
103+
let lastName = "Williams";
104+
let oldName = lastName;
105+
lastName = "Davis";
106+
console.log(lastName, oldName);
107+
108+
// Refrence Types - Heap
109+
const jessica = {
110+
firstName: "Jessica",
111+
lastName: "Williams",
112+
age: "27",
113+
};
114+
115+
const marriedJessica = jessica;
116+
marriedJessica.lastName = "Davis";
117+
console.log("Before Marraige", jessica);
118+
console.log("After Marraige", marriedJessica);
119+
120+
// marriedJessica = {};
121+
122+
// Copying Objects
123+
const jessica2 = {
124+
firstName: "Jessica",
125+
lastName: "Williams",
126+
age: "27",
127+
128+
family: ["Alice", "Bob"],
129+
};
130+
131+
const jessicaCopy = Object.assign({}, jessica2);
132+
jessicaCopy.lastName = "Davis";
133+
134+
jessicaCopy.family.push("Mary");
135+
jessicaCopy.family.push("John");
136+
137+
console.log("Before Marraige", jessica2);
138+
console.log("After Marraige", jessicaCopy);

0 commit comments

Comments
 (0)