About "this" keyword/variable in Javascript

 1) JavaScript this:

In JavaScript, this keyword refers to the object where it is called.


2)1. this Inside Global Scope

When this is used alone, this refers to the global object (window object in browsers).

let a = this;
console.log(a); // Window {}


this.name = 'Swati';
console.log(window.name); // Swati











3)2. this Inside Function

When this is used in a function, this refers to the global object (window object in browsers).


function greet() {
// this inside function
// this refers to the global object
console.log(this);
}
greet(); // Window {}















4)3. this Inside Constructor Function

In JavaScript, constructor functions are used to create objects. When a function is used as a constructor function, this refers to the object inside which it is used.


function Person() {
this.name = "Swati";
console.log(this);
}
let person1 = new Person();
console.log(person1.name);









Note: When this is used with ES6 classes, it refers to the object inside which it is used(similar to constructor functions).


5)4. this Inside Object Method

When this is used inside an object's method, this refers to the object it lies within. 


const person = {
name: "Swati",
age: 25,


// this inside method
// this refers to the object itself
greet() {
console.log(this);
console.log(this.name);
},
};


person.greet();









6)5. this Inside Inner Function

When you access this inside an inner function (inside a method), this refers to the global object. 


const person = {
name: "Swati",
age: 25,
// this inside method
// this refers to the object itself
greet() {
console.log(this); // {name: "Swati", age ...}
console.log(this.age); // 25
// inner function
function innerFunc() {
// this refers to the global object
console.log(this); // Window { ... }
console.log(this.age); // undefined
}
innerFunc();
},
};
person.greet();










7)6. this Inside Arrow Function

Inside the arrow function, this refers to the parent scope. Arrow functions do not have their own this. When you use this inside an arrow function, this refers to its parent scope object.


Example 1:

const greet = () => {
console.log(this);
}
greet(); // Window {...} basically in the browser console it will give window in vs code its not giving the correct output thats why I haven't taken screenshot of the terminal for this example

Example 2:

let greet = {
name: "Swati",


sayHi() {
let hi = () => console.log(this.name);
hi();
},
};
greet.sayHi();







Example 3:

let person = {
name: 'Swati',
greet() {
console.log(this);
console.log(this.name);


let innerFunc = () => {
console.log(this);
console.log(this.name);
}
innerFunc();
}
}
person.greet();










8) 7. this Inside Function with Strict Mode

When this is used in a function with strict mode, this is undefined. 


"use strict";
this.name = "Swati";
function greet() {
// this refers to undefined
console.log(this);
}
greet(); // undefined










Note: When using this inside a function with strict mode, you can use JavaScript Function call().

'use strict';
this.name = 'Swati';


function greet() {
console.log(this.name);
}


greet.call(this); // Swati







The call() method calls a function by passing this and specified values as arguments.


9)Static Methods:


Static class methods are defined on the class itself.

You cannot call a static method on an object, only on an object class.


class User {
// constructor(name1){
// this.name1 = name1;
// }

static getUser(x) {
return "Hello" + x;
}
}
//let object = new User("Swati");
//console.log(object.getUser());
console.log(User.getUser("Swati"));










Comments

Popular posts from this blog

Object.getOwnPropertyDescriptor & Object.defineProperty Methods.

Scope and Closures / Callback Hell

DOM - Document Object Model (document.getElementById etc.)