With the following code, how can the method my_method be called so that it prints the global object (window) ?
MyClass = function() {
this.my_method = function() {
console.log(this);
}
}
var myobject = new MyClass();
This is not an homework, but a question that I’ve been asked and that I can’t understand because i don’t know in deep JavaScript.
So, if someone can help me understand and give me a well explained answer I will be happy !
You can bind the function to null :
That’s because when the context of a function is null or undefined,
thisis the global object.From the ECMAScript reference :