2013-10-02 29 views
6

所以我要鏈接每一個需要到index.html文件文件:不能訪問的變量在另一個javascript文件

<script src="jquery.js"></script> 
    <script src="notify.js"></script> 
    <script src="script.js"></script> 

我在「notify.js」創建一個對象:

var notify = { 
    newNotification : function(text) { 
    } 
} 

的script.js:

alert(notify.newNotification); 

當我嘗試訪問「的script.js」的「通知」的對象,它只是fine.But我想使用jquer Ÿ所以我加$(文件)。就緒()這兩個文件是這樣的:

notify.js

$(document).ready (
    function() { 
var notify = { 
    newNotification : function(text) { 
    } 
} 
} 
) 

的script.js:

$(document).ready (
    function() { 
alert(notify.newNotification); 
    } 
) 

後,我補充一點, ,它提出通知未定義。有什麼不對?任何人都可以解釋爲什麼它不起作用?

+0

你爲什麼要定義'notify'在'$ .ready'範圍是什麼?僅僅因爲你「想要使用jQuery」,DOM準備就沒有必要。 – Bergi

+0

這似乎是對jQuery的錯誤使用。 @紅石套件,你想要達到什麼目標? – stavarotti

+0

http://stackoverflow.com/questions/111102/how-do-javascript-closures-work – porfiriopartida

回答

7

正如您在中定義的notify.js裏面的$(document).ready(這是一個匿名函數而var notify範圍僅限於此函數。

所以它不是$(document).ready(函數外部訪問

爲了讓外部訪問的$(document).ready(功能

這樣不把它包裝: -

var notify; 
$(document).ready(function() { 
    notify = { 
     newNotification: function (text) { } 
    } 
}); 
0

在這種情況下,沒有需要將通知對象封裝在DOM中...因爲從它的外觀來看,您在創建對象時不會創建任何DOM元素引用......唯一重要的是任何涉及DOMOM elem的方法調用ent必須在dom做好準備。

var notify = { 
    newNotification: function (text) {} 
} 

$(document).ready(function() { 
    notify.newNotification() 
}) 

如果聲明一個DOM準備處理程序裏面的變量,然後就變成了局部變量的DOM準備處理......所以它不會是DOM準備處理外部訪問...

另一個解決方案是將DOM準備手柄內的變量添加到全球範圍,想

var notify; 
$(document).ready(function() { 
    notify = { 
     newNotification: function (text) {} 
    } 
}) 

$(document).ready(function() { 
    window.notify = { 
     newNotification: function (text) {} 
    } 
}) 
0

您只需要一個文檔。已準備好 而且這隻能聲明可在腳本中自由移動的變量。

參見例如:

的script.js:

$(document).ready(function(){ 
    // declare the variables as global 
    $.varA, $.varB, $.varC; 
}); 

通知。js:

function doNotify(){ 
    // your code here 
    $.varA = /*your code here */ 
} 

function otherFunc(txt){ 
    // your code here 
    $.varB = txt; 
} 
-1

試試這個。

var notify = { 
    newNotification : function(text) { 
    } 
0

在文檔準備好之前,所有的JavaScript都會加載。

創建script.js一個單獨的函數引用notify對象,然後調用該函數從$(document).ready

2

像其他人一樣在這裏已經指出:僅使用$().ready當你處理DOM元素和你的變量不因爲您使用了var關鍵字(就像您應該這樣做)。 var關鍵字將定義的變量限制在當前範圍內,這是您用作DOM-Ready-Handler的匿名函數的範圍。

因此,刪除不必要的$().read將暫時解決您的問題。

但是(!)你應該把你的代碼封裝到閉包中以避免搞亂全局範圍,並避免與第三方代碼可能的命名衝突。

就像是:

notify.js

;(function ($, window, undefined) { 
    var notify = { 
    newNotification : function(text) { 
     return text; 
    } 
    }; 
})(jQuery, this); 

的script.js

;(function ($, window, undefined) { 
    alert(notify.newNotification()); 
})(jQuery, this); 

所以,現在你要像前有同樣的問題,你沒有訪問到你的對象。

當然,您可以將您的notify對象添加到全局範圍,因爲Arun P Johny在他的答案中提出了建議,但我很確定在這段時間內會有更多的對象需要讓全局訪問。 如果你把它們放在全局範圍內,你就會再次搞亂全局範圍,所以我的建議是一個全局對象,它將容納你需要全局訪問的所有其他對象/變量。 (甚至更好使用類似requirejs

Somethink這樣的:

main.js

;var MyApp = {}; # Only variable in global scope 

# Maybe some more initalization code here, dunno 

notify.js

;(function ($, window, undefined) { 
    MyApp.notify = { 
    newNotification : function(text) { 
     return text; 
    } 
    }; 
})(jQuery, this); 

腳本。JS

;(function ($, window, undefined) { 
    alert(MyApp.notify.newNotification()); 
})(jQuery, this); 

一些有趣的Q/A的有關計算器範圍和封鎖這裏:

一個很好的回答與亂搞全球範圍內: