-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
53 lines (41 loc) · 1.07 KB
/
Copy paththis.js
File metadata and controls
53 lines (41 loc) · 1.07 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
50
51
52
"use strict";
// this is global scope
console.log("test123=>",this); // globalObject - Print window obj in browswer, global obj in nodejs
// this is inside function
function x() {
// the values depneds upon strict mode / non strict mode
// strict mode display undefined
// non strict mode display window object
console.log("test=>",this);
}
x();
window.x(); // display windows object in strict mode
const thisobj = {
a:10,
print: function () {
console.log(this.a);
}
}
thisobj.print(); // print 10; Because here this refer to thisobj
// call apply bind is sharing methods
const student = {
name: "Virat",
printName: function() {
console.log(this.name);
}
}
student.printName(); // Print Virat
const student2 = {
name: "Kohli"
}
student.printName.call(student2); // Print Kohli
const obj = {
p: 10,
printnumber: function () {
const y = () => {
console.log("arrow=>",this); // Print Window Object because it is inside enclosing lexical context
}
y();
},
}
obj.printnumber();