Method invocation
suggest changeCalling 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents