-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththis.js
38 lines (33 loc) · 859 Bytes
/
this.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
/**************************
* this keyword examples starts from here,
* the this keyword points at the global object, (the window object,
* in the browser or coding window object)
*/
console.log(this);
//-----------------
calculateAge(1993);
function calculateAge(year) {
console.log(2019 - year);
console.log('line 13', this);
}
//---------------------------
var john = {
name: 'Chethan',
yob: 1993,
calculateAge: function () {
console.log('20', this); // this keyword refers to john object
console.log(2019 - this.yob);
function innerObj() {
console.log('23', this); // refers to window object
}
innerObj();
}
}
john.calculateAge();
//------------method barowing
var mike = {
name: 'Vignesh',
yob: 1991
}
mike.calculateAge = john.calculateAge;
mike.calculateAge();