Method invocation

suggest change

Calling a function as a property of an object sets this to that object. Taking the function out and calling it separately does not. In strict mode that detached call has this === undefined. In non-strict code it is the global object.

const obj = {
    name: "Foo",
    print: function () {
        console.log(this.name)
    }
}

We can now invoke print as a method of obj. this will be obj

obj.print();

This will thus log:

Foo

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents