-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path03_this_keyword.js
44 lines (33 loc) · 897 Bytes
/
03_this_keyword.js
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
"use strict";
console.log(this);
// In strict mode - inside regular function **this** object is going to be undefined
// In non strict mode **this** object is going to be the window object
const calcAge = function(birthYear) {
console.log(2037 - birthYear);
console.log(this);
}
calcAge(1991);
// In strict mode - inside arrow function **this** object is going to be window object
// In arrow function it uses the this object of parent function
const calcAgeArrow = (birthYear) => {
console.log(2037 - birthYear);
console.log(this);
}
calcAgeArrow(1994);
const jonas = {
year: 1991,
calcAge: function() {
// Jonas object
console.log(this);
console.log(2037 - this.year);
}
}
jonas.calcAge();
const matilda = {
year: 2017,
};
// Method borrowing
matilda.calcAge = jonas.calcAge;
matilda.calcAge();
const f = jonas.calcAge;
// f();