-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththis.js
71 lines (56 loc) · 1.74 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// this in global scope
console.log(this); // global object
// window in case of browser
// global in case of node
// this inside a function
// in non strict mode it is different (default): value of this is window
// in strict mode it is different: value of this is undefined
// value of this depends on non strict and strict mode
function x(){
console.log(this);
}
// this substitution
// If this keyword value is undefined or null
// then it is replaced with global object
// in non strict mode
//this keyword value depends on how the function is called
// when function is called with reference value this becomes global object
// else undefined
x();
// in this case it is window object, x is called with window reference
window.x()
// this inside a object's method
const student = {
name: "Abhi",
printName: function(){
console.log(this.name);
}
}
// value of this is object itself {name:"Abhi", printName : f}
// Iit will print Abhi
obj.printName();
// call apply bind methods (sharing methods)
const student2 = {
name: "Raj",
}
// by overriding the value of this keyword
// value of this is student2
// and it will print Raj
student.printName.call(student2);
// this inside arrow function
// arrow function does not have this keyword
// it retains the this value of the enclosing lexical context(env) (where the function is defined)
let obj = {
a: 10,
x : ()=>{
// here this obj is defined in global scope
// so it retains the value of this of global scope
// so here the value of this is window object
console.log(this);
}
}
// this inside dom
// the value of this is reference to html element
// example
// value of this is button itself
{/* <button onclick="alert(this)">btn</button> */}