Note: the code in this question was run in Chrome Console.
I encountered this problem when I was doing the JS-puzzler, question 21 (well.. it didn't gave a ordering though). The question ask about the result of:
var x = [].reverse; x();
and the answer is window. As the answer states:
[].reverse will return this and when invoked without an explicit receiver object it will default to the default this AKA window.
Based on this understanding, I wrote a bit of code to test:
function Bar(){
var x = [].reverse;
console.log(x());
}
new Bar();
And guess what.. this code raise an error:
TypeError: Array.prototype.reverse called on null or undefined
I want to ask why the x() called in the new Bar is not showing the this object, but raising exception instead?
var x = [].reverse; x();throws the same error. I don't know why it waswindowfor you :(