var a = 1;
var b = {
a : 2,
c : function () {
console.log(this.a);
}
};
b.c(); // logs 2
(b.c)(); // logs 2
(0, b.c)(); // logs 1
Run Code Online (Sandbox Code Playgroud)
第一个是可以理解的,因为"this"指的是Object"b".但为什么第二个会记录相同的结果呢?我认为"this"应该指向全局执行上下文.第三个,似乎逗号运算符影响执行上下文.
有时我盯着google.com主页提供的js,发现他们倾向于使用(0, obj.func)(args)语法.以下是该剧本的摘录:
var _ = _ || {};
(function (_) {
var window = this;
try {
_.mb = function (a) {
return (0, window.decodeURIComponent)(a.replace(/\+/g, " "))
};
_.zg = function (a, b) {
for (var c = a.length ? a.split("&") : [], d = 0; d < c.length; d++) {
var e = c[d];
if ((0, _.Ag)(e) == b) return (c = /=(.*)$/.exec(e)) ? (0, _.mb)(c[1]) : null
}
return null
};
_.Ag = function (a) { …Run Code Online (Sandbox Code Playgroud)