How to use the ‘this’ Keyword in Object In Javascript
The ‘this’ keyword in JavaScript refers to the current object context within which the code is executed. It typically refers to the object on which a method is called.
Example: Explanation of Structs in JavaScript Using the ‘this’ Keyword in Object.
const person = {
name: 'John',
greet: function () {
console.log('Hello, my name is ' + this.name);
}
};
person.greet();
Output
Hello, my name is John
How to work with Structs in JavaScript ?
Structs are typically found in languages like C, C++, and similar, and they provide a way to organize related data items under one name. Typically JavaScript does not have built-in support for structs, but you can achieve similar functionality using objects. Objects in JavaScript are dynamic collections of key-value pairs, where keys are strings (or Symbols) and values can be any data.
Table of Content
- Using Objects
- Using ES6 Classes
- Using String Split()
- Using the ‘this’ Keyword in Class
- Using the ‘this’ Keyword in Object