在js中,为什么(0,fn)()这样就可在全局中执行fn这个函数?

阅读 5.8k
2 个回答

由于逗号操作符,
会把最右边的赋值给()里,
即(0, _b.a)(); 等价于 var method = _b.a;
然后立即执行,method();
此时执行a方法 的对象已经变为 window了。a中的this 都指向window了。

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

逗号操作符从左往右执行,并且反悔最后一个操作对象的值(值而不是引用);

推荐问题