I am curious as to why this works:
var c = {
d: function myFunc() {
console.log(this === window);
}
};
var a = {
b: function() {
console.log(this === a);
(0,c.d)();
c.d();
}
};
a.b();
Console output:
True
True
False
So it seems to be that (0, c.d)() is the same as c.d.call(window), but I cannot seem to find much about why or how this works. Can anyone explain?
From: Closure Compiler Issues
Fiddle: http://jsfiddle.net/wPWb4/2/
If you write multiple expressions separated by a comma (
,), then all expressions will be evaluated, but you will end up with the value of the last expression:Now
(0,c.d)is an expression that will return the functionc.d, but nowcis not thethisof the function anymore. This means thatthiswill point to the global object (window), or remain undefined in strict mode. You would get the same effect with any of these:Or just