DEV Community

Discussion on: JavaScript's 'this' Keyword: Understanding Its Power and Usage

Collapse
 
wiseai profile image
Mahmoud Harmouch

Awesome tutorial! I just want to highlight an important ES6 feature you might find helpful. In modern JavaScript, you no longer need to explicitly type the word "function" when defining functions inside objects. It is also cross-browser compatible 1. So, your example can be simplified like this:

const person = {
  name: 'John',
  greet() {
    console.log(this.name); // 'this' points to the 'person' object
  }
};
person.greet(); // Output: John
Enter fullscreen mode Exit fullscreen mode

  1. You can learn more about this feature in the Mozilla Developer Network documentation: developer.mozilla.org/en-US/docs/W... 

Collapse
 
adii profile image
Aditya Singh

The ES6 feature you've mentioned is known as "method shorthand," and it indeed simplifies the process of defining functions inside objects. Thanks for sharing this valuable information!