getUserMail
未在scope中定義,calljs
可訪問該scope。這就是爲什麼你得到ReferenceError
;在您嘗試調用getUserMail
的上下文中,沒有可用該名稱的函數。
// At this point nothing is defined
function calljs(){
getUserMail(usermMail);
}
// now calljs is defined as a global and can be invoked from anywhere
$(function() { // this line is calling a function named $ (an alias for jQuery)
// and passing it an anonymous function as a parameter.
function getUserMail(usermMail) { // This function is being defined inside
// the scope of the anonymous function,
// it can be used anywhere inside the
// anonymous function but not outside it.
// ***some code*****
}
});
// we are now outside the scope of the anonymous function,
// getUserMail is no longer in our scope and can't be called from here.
在大多數情況下,最簡單的和可能最好的辦法是,以確保相互調用的任何功能都在同一範圍內。
從我能告訴你真的不需要calljs
,你只是試圖用它來戳一個洞,在其中定義了getUserMail
的匿名函數的範圍。
相反,你應該擺脫calljs
和移動正在調用getUserMail
的ready
回調的任何代碼。如果getUserMail
在調用之前需要等待ready
回調被觸發,那麼調用它的任何代碼也應該在ready
回調中。 (像事件處理程序,它稱爲它應該已經在ready
回調裏面)
如果有一個原因,你不能將它移動到ready
回調,如另一個.js文件中的東西需要能夠稱之爲等等,你的應用程序可能太複雜,不能像jQuery soup那樣被實際維護。將它移植到框架如Ember或Angular可能是值得的。
也爲了便於掌握,您的script
標籤上有no need to use the type
attribute。JavaScript是唯一在瀏覽器中擁有廣泛支持的語言,所有瀏覽器默認使用JavaScript來代替script
標籤。
對於'calljs'函數,沒有'getUserMail'函數來訪問。 – 2014-10-20 06:10:30
它不能從另一個函數中調用另一個函數中的函數。 – Ravi 2014-10-20 06:13:00
如果可能的話從java腳本中調用jquery函數。 – nmkkannan 2014-10-20 06:14:09