Share
Facebook
Twitter
LinkedIn
Whatsapp
Instagram
Messenger

জাভাস্ক্রিপ্ট


In JavaScript, the "this" keyword refers to the current object or context in which the code is being executed. The value of "this" can change depending on how a function is called or where it is used within the code. When used within an object, "this" refers to that object itself and can be used to access its properties and methods. In a function that is not part of an object, the value of "this" will be the global object, which is typically the window object in a browser environment. Understanding how "this" works is important for writing effective and maintainable JavaScript code.


What is the this keyword?

In JavaScript, the this keyword refers to the execution context of a function. The value of this can vary depending on how the function is called. The this keyword allows you to access the properties and methods of the object that called the function.


How is this used?

this is used to access the properties and methods of an object. In a method, this refers to the object that the method belongs to. In a function, this refers to the global object (in a browser, this is the window object). Let's take a look at some examples to illustrate how this works.


Method invocation

When a method is called on an object, this refers to the object that the method belongs to. Here's an example:

উদাহরণ


const person = {
  name: 'John',
  age: 30,
  greet() {
    console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`);
  }
};

person.greet(); // logs "Hi, my name is John and I am 30 years old."

কোড এডিটর


In this example, the greet() method is called on the person object. Inside the method, this refers to the person object, so we can access the name and age properties using this.


Function invocation

When a function is called without an object, this refers to the global object. Here's an example:

উদাহরণ


function sayHello() {
  console.log(`Hello, my name is ${this.name}.`);
}

const person = {
  name: 'John',
  age: 30,
  greet: sayHello
};

person.greet(); // logs "Hello, my name is undefined."

কোড এডিটর


In this example, the sayHello() function is called on the person object. However, inside the function, this refers to the global object, not the person object. This is why we get undefined when we try to access the name property using this.


Constructor invocation

When a function is called with the new keyword, this refers to the newly created object. Here's an example:

উদাহরণ


function Person(name, age) {
  this.name = name;
  this.age = age;
}

const john = new Person('John', 30);

console.log(john); // logs { name: 'John', age: 30 }

কোড এডিটর


In this example, we define a Person constructor function that takes two arguments: name and age. When we call the Person function with the new keyword, a new object is created with name and age properties. Inside the Person function, this refers to the newly created object.


Call or apply invocation

When a function is called using the call() or apply() method, this refers to the first argument passed to the method. Here's an example:

উদাহরণ


function sayHello() {
  console.log(`Hello, my name is ${this.name}.`);
}

const person = {
  name: 'John',
  age: 30,
};

sayHello.call(person); // logs "Hello, my name is John."

কোড এডিটর


In this example, we call the sayHello() function using the call() method and pass the person object as the first argument. Inside the