gpt4 book ai didi

javascript - 为什么谷歌主页使用 (0, obj.func)(args) 语法?

转载 作者:行者123 更新时间:2023-12-03 03:49:04 26 4
gpt4 key购买 nike

有时我盯着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) {
return (a = /^(.+?)(?:=|$)/.exec(a)) ? (0, _.mb)(a[1]) : null
};
var Cg = function (a, b) {
var c = a.indexOf("?");
return 0 > c ? null : (0, _.zg)(a.substring(c + 1), b)
};
// Note var Cg called with no 0
var oca = function (a) {
this.A = Cg(a, "mods");
this.B = Cg(a, "ver")
};
} catch (e) {}
})(_);

为什么前面要加0?

最佳答案

这使得 indirect call .

这确保了被调用函数中的上下文是全局的。这在内部范围内可能很有用。

示例:

var a = {
b: function(){
console.log(this);
},
c1: function(){
this.b();
},
c2: function(){
(0, this.b)();
},
c3: function(){
(this.b)();
}
}
a.c1(); // logs a
a.c2(); // logs window
a.c3(); // logs a

关于javascript - 为什么谷歌主页使用 (0, obj.func)(args) 语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19535601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com