-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_this_keyword_advanced.js
More file actions
64 lines (52 loc) · 2.39 KB
/
04_this_keyword_advanced.js
File metadata and controls
64 lines (52 loc) · 2.39 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
53
54
55
56
57
58
59
60
61
62
63
// This code demonstrates the use of 'this' in a regular function and an arrow function.
// The 'this' keyword behaves differently in regular functions and arrow functions.
// In regular functions, 'this' refers to the object that calls the function.
// Regular function
function regularFunction() {
console.log(this);
}
// In non-strict mode, 'this' refers to the global object (window in browsers). in Node.js).
// In strict mode, 'this' will be undefined.// If you want to see the difference, you can uncomment the line below to enable strict mode.
// 'use strict'; // Uncomment this line to enable strict mode
// Calling the regular function
// regularFunction(); // In non-strict mode, this will refer to the global object (window
// In strict mode, this will be undefined
// Arrow function
// In arrow functions, 'this' is lexically bound, meaning it takes 'this' from the surrounding context.
// In this case, it will refer to the global object (window in browsers) or undefined
// in strict mode.
// Arrow function
const arrowFunction = () => {
console.log(this);
};
// Calling the arrow function
// arrowFunction(); // In non-strict mode, this will refer to the global object (window in browsers) or undefined in strict mode.
// Example of 'this' in an object method
// In an object method, 'this' refers to the object itself.
// This is a simple object with a method that uses 'this' to refer to its properties.
// Example of 'this' in an object method
// In an object method, 'this' refers to the object itself.
const user = {
userName: "Ashish Choudhary",
userAge: 29,
userDetails: function () {
console.log(this.userName + " is " + this.userAge + " years old.");
console.log(this);
},
};
// user.userDetails(); // This will log "Ashish Choudhary is 29 years old." and the user object itself.
function userOne(userName, userAge, loginStatus) {
this.userName = userName;
this.userAge = userAge;
this.loginStatus = loginStatus;
return this;
// this.userDetails = function () {
// console.log(this.userName + " is " + this.userAge + " years old.");
// console.log(this);
// };
}
// const userOneobj1 = userOne("Ashish Choudhary", 29, true); // without new value is overhide ocinstractear fucntion
const userOneobj1 = new userOne("Ashish Choudhary", 29, true);
const userOneobj2 = new userOne("khushi Choudhary", 25, false);
console.log(userOneobj1);
console.log(userOneobj2);