I have the following code. It’s a JavaScript module.
(function() {
// Object
var Cahootsy;
Cahootsy = {
hello: function () {
alert('test');
},
};
(Cahootsy.scope = (function() {
return this;
})()).Cahootsy = Cahootsy;
return Cahootsy;
}).call(this);
I don’t understand the section:
(Cahootsy.scope = (function() {
return this;
})()).Cahootsy = Cahootsy;
I think it is creating an object referencing ‘this’ module and then assigns the Cahootsy variable to a global Cahootsy variable. What I don’t understand is why ‘this’ needs to be assigned to Cahootsy.scope
You can break it down a little, like so:
What it does is gets the global scope (usually
window, but not always). Then it creates a link fromCahootsyto the global scope via the object’sscopeproperty, and a link the other way via the scope’sCahoostyproperty.The result is that, in a browser, you would get
window.Cahootsyis the object, andwindow.Cahootsy.scopegoes back to the window.