I was curious about this as well as I had just seen John Resig's talk about this video. Yoshi had a great answer but I had to test it in a bit in console log to understand and I thought this modification to his answer might help some people who were having trouble at first like me:
function Foo() {
this.foo = true;
(function () {
console.log("Foo = " + this.foo);
// Outputs undefined
}());
(function () {
console.log("Foo = " + this.foo);
// Outputs true
}).call(this);
(function () {
console.log(this);
// Outputs undefined in strict mode, or Window in non strict mode
// Anonymous functions usually default to the global scope
})();
}
var bar = new Foo;
It just made a little more sense to me to see the first and second ones side by side, showing that .call(this) essentially gives you the ability to pass the current context to an anonymous function.
Thanks for the question and thanks Yoshi for the clear answer!