this

this is a object reference to its parent object.

  • In creation phase this points to window object.

  • this will represents the object it belongs to.

Ex 1:
    console.log(this) // window
    this === window // true
    
    function a() {
        console.log(this) // window
        this === window // true
    }
    
    a(); // same as window.a();

In example 1 the function "a" is belongs to window object, so this is reference of window object

Ex 2:
const greet = {
    welcome: function() {
        console.log(this) // greet
        this === window // false
    }
};

greet.welcome();

In example 2 the function "welcome" is belongs to greet object, this is reference of greet object, so it is not same as window.

In example 3 "isWindow" property of greet2 object is "true", because greet2 belongs to window object. "this" reference changed for only inside the function of the objects.

Last updated

Was this helpful?