Aşağıdaki koda sahibim. Bu bir JavaScript modülü.Javascript Modülleri
(Cahootsy.scope = (function() {
return this;
})()).Cahootsy = Cahootsy;
Ben 'bu' modülü başvuran bir nesne oluşturup ardından küresel Cahootsy değişkene Cahootsy değişkeni atar düşünüyorum:
(function() {
// Object
var Cahootsy;
Cahootsy = {
hello: function() {
alert('test');
},
};
(Cahootsy.scope = (function() {
return this;
})()).Cahootsy = Cahootsy;
return Cahootsy;
}).call(this);
ben bölümü anlamıyorum. Anlamadığım şey, 'bu' nedeninin Cahootsy.scope
Modül neden global kapsam için bir referansa sahip olur? Ayrıca, neden sadece Cahootsy.scope = window 'yapmak değil? –
Çünkü bahsettiğim gibi, 'window' her zaman global kapsam değildir. Örneğin, JavaScript Node.JS üzerinde çalıştırıldığında, tarayıcı yoktur ve bu nedenle "window" yoktur. –
Bu, küresel kapsamı elde etmek için hacky bir yol gibi görünüyor! :) –