|
| 1 | +// This code demonstrates the use of 'this' in a regular function and an arrow function. |
| 2 | + |
| 3 | +// The 'this' keyword behaves differently in regular functions and arrow functions. |
| 4 | +// In regular functions, 'this' refers to the object that calls the function. |
| 5 | +// Regular function |
| 6 | +function regularFunction() { |
| 7 | + console.log(this); |
| 8 | +} |
| 9 | +// In non-strict mode, 'this' refers to the global object (window in browsers). in Node.js). |
| 10 | +// In strict mode, 'this' will be undefined.// If you want to see the difference, you can uncomment the line below to enable strict mode. |
| 11 | +// 'use strict'; // Uncomment this line to enable strict mode |
| 12 | +// Calling the regular function |
| 13 | +// regularFunction(); // In non-strict mode, this will refer to the global object (window |
| 14 | +// In strict mode, this will be undefined |
| 15 | + |
| 16 | +// Arrow function |
| 17 | +// In arrow functions, 'this' is lexically bound, meaning it takes 'this' from the surrounding context. |
| 18 | +// In this case, it will refer to the global object (window in browsers) or undefined |
| 19 | +// in strict mode. |
| 20 | +// Arrow function |
| 21 | +const arrowFunction = () => { |
| 22 | + console.log(this); |
| 23 | +}; |
| 24 | +// Calling the arrow function |
| 25 | +// arrowFunction(); // In non-strict mode, this will refer to the global object (window in browsers) or undefined in strict mode. |
| 26 | +// Example of 'this' in an object method |
| 27 | +// In an object method, 'this' refers to the object itself. |
| 28 | +// This is a simple object with a method that uses 'this' to refer to its properties. |
| 29 | + |
| 30 | +// Example of 'this' in an object method |
| 31 | +// In an object method, 'this' refers to the object itself. |
| 32 | + |
| 33 | +const user = { |
| 34 | + userName: "Ashish Choudhary", |
| 35 | + userAge: 29, |
| 36 | + userDetails: function () { |
| 37 | + console.log(this.userName + " is " + this.userAge + " years old."); |
| 38 | + console.log(this); |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +// user.userDetails(); // This will log "Ashish Choudhary is 29 years old." and the user object itself. |
| 43 | + |
| 44 | +function userOne(userName, userAge, loginStatus) { |
| 45 | + this.userName = userName; |
| 46 | + this.userAge = userAge; |
| 47 | + this.loginStatus = loginStatus; |
| 48 | + |
| 49 | + return this; |
| 50 | + |
| 51 | + // this.userDetails = function () { |
| 52 | + // console.log(this.userName + " is " + this.userAge + " years old."); |
| 53 | + // console.log(this); |
| 54 | + // }; |
| 55 | +} |
| 56 | + |
| 57 | +// const userOneobj1 = userOne("Ashish Choudhary", 29, true); // without new value is overhide ocinstractear fucntion |
| 58 | +const userOneobj1 = new userOne("Ashish Choudhary", 29, true); |
| 59 | +const userOneobj2 = new userOne("khushi Choudhary", 25, false); |
| 60 | + |
| 61 | + |
| 62 | +console.log(userOneobj1); |
| 63 | +console.log(userOneobj2); |
| 64 | + |
0 commit comments