The backbone.js source code uses a function wrapper like this:
(function(){
...
}).call(this);
as seen at http://backbonejs.org/docs/backbone.html#section-185.
Much more often, I've seen the following used instead:
(function(){
...
})();
When does the behavior of these two differ? I was under the impression that they were equivalent, but I assume that there must be a difference given that Backbone uses .call(this)
instead of the shorter alternative.
In the first example, this
inside of the function will be the this
from the calling scope.
In the second example, this
will be window
.
(As noted by Šime Vidas, it's undefined
in strict mode, rather than window
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With