我有以下代码。它是一个 JavaScript 模块。
(chức năng() {
// Object
var Cahootsy;
Cahootsy = {
hello: function () {
alert('test');
},
};
(Cahootsy.scope = (function() {
return this;
})()).Cahootsy = Cahootsy;
return Cahootsy;
}).call(this);
我不明白的部分:
(Cahootsy.scope = (function() {
return this;
})()).Cahootsy = Cahootsy;
我认为它正在创建一个引用“this”模块的对象,然后将 Cahootsy 变量分配给全局 Cahootsy 变量。我不明白的是为什么需要将“this”分配给 Cahootsy.scope
你可以把它分解一点,像这样:
var getScope = function() {return this;}
Cahootsy.scope = getScope();
getScope().Cahootsy = Cahootsy;
它所做的是获取全局范围(通常是 window
,但并非总是如此)。然后它从 Cahootsy
创建一个链接通过对象的 scope
到全局范围属性,并通过作用域的 Cahoosty
以另一种方式链接属性(property)。
结果是,在浏览器中,您将得到 window.Cahootsy
是对象,window.Cahootsy.scope
回到窗口。
Tôi là một lập trình viên xuất sắc, rất giỏi!