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