Objects Can Act

A method is a function that is a property of an object.

let user = {
  name: "John",
  sayHi() {
    alert(this.name); // 'this' refers to the current object
  }
};

The value of " this "

In JavaScript, the value of this is evaluated at call-time. It depends on which object is " before the dot " when the function is called.

Caution

In " strict mode " (use strict), if a function is called without an object (just func()), this will be undefined. In non-strict mode, it would be the global window object.

Important

Arrow functions DO NOT have their own this. If you use this inside an arrow function, it is taken from the outer normal function.