Why is .call(this) used instead of parenthesis

javascript

Solution

Presumably the code within that function uses `this` (where you just have `console.log`). In the version with `call`, `this` within the function is the same as `this` outside it. Without `call`, `this` inside the function is either the global object (loose mode) or `undefined` (strict mode).

If you're not using `this` within the function, there's no reason to be doing the `call` version, and I would lean toward not doing so because it's additional unnecessary complexity (and apparently a very very small performance cost).

Problem

Is there a particular reason why i often encounter: ``` (function() { console.log("Hello"); }).call(this); ``` instead of: ``` (function() { console.log("Hello"); })(); ``` It should have the same effect when passing `this` to call or not? There seems to be some performance difference: http://jsperf.com/call-vs-parenthesis.

Original source

Related problems