Javascript模块

前端之家收集整理的这篇文章主要介绍了Javascript模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码这是一个 JavaScript模块. @H_403_2@(function() { // Object var Cahootsy; Cahootsy = { hello: function () { alert('test'); },}; (Cahootsy.scope = (function() { return this; })()).Cahootsy = Cahootsy; return Cahootsy; }).call(this);

我不明白的部分:

@H_403_2@(Cahootsy.scope = (function() { return this; })()).Cahootsy = Cahootsy;

我认为它正在创建引用“this”模块的对象,然后将Cahootsy变量分配给全局Cahootsy变量.我不明白为什么“这”需要分配给Cahootsy.scope

解决方法

你可以把它分解一下,就像这样: @H_403_2@var getScope = function() {return this;} Cahootsy.scope = getScope(); getScope().Cahootsy = Cahootsy;

它的作用是获得全局范围(通常是窗口,但不总是).然后,它通过对象的scope属性创建从Cahootsy到全局范围的链接,另一个方法通过范围的Cahoosty属性创建一个链接.

结果是,在浏览器中,你会得到window.Cahootsy是对象,而window.Cahootsy.scope返回到窗口.

猜你在找的JavaScript相关文章