Talk is cheap; show me the code.
// equals to this.test = "inside window"
var test = "inside window";
function f () {
console.log(this.test)
};
var obj = {
test: "inside object",
fn: f
};
obj.fn(); // "inside object" --> fine
(obj).fn(); // "inside object" --> fine
(1, obj).fn(); // "inside object" --> fine
(obj.fn)(); // "inside object" --> fine
(0 || obj.fn)(); // "inside window" --> why?
// reference equality check
console.log(
f === obj.fn &&
(obj.fn) === f &&
f === (1, obj.fn)
); // all equal :/
I had read ydkjs book and Im familiar with call-site and dynamic this binding, but I don't understand why the last function call has window as its this context; in this controlled experiment that only thing
that is changed () and comma operator and as you can see in the last statement comma operator is doing something weird. I suspect it does an assignment when it returns the value (since if we do an assignment the same result happens) but I'm not sure.
UPDATE:
The effect is also seen on &&, || operators: (0 || obj.fn)()