Javascript 范围困难
我有谷歌地图的javascript代码,但没有表现出来,我认为这与范围有关,但我无法理解。以下是相关代码:Javascript 范围困难,javascript,google-maps,scope,Javascript,Google Maps,Scope,我有谷歌地图的javascript代码,但没有表现出来,我认为这与范围有关,但我无法理解。以下是相关代码: function initialize() { google.maps.event.addListener(MapArea, 'click', showIW); function showIW(event) { function moreInfo(){ //... } infoWindow.setC
function initialize() {
google.maps.event.addListener(MapArea, 'click', showIW);
function showIW(event) {
function moreInfo(){
//...
}
infoWindow.setContent(moreInfo());
}
}
google.maps.event.addDomListener(window, 'load', initialize);
这不起作用(moreInfo未定义),但是,它可以:
function initialize() {
google.maps.event.addListener(MapArea, 'click', showIW);
function showIW(event) {
infoWindow.setContent(moreInfo());
}
}
function moreInfo(){
//...
}
google.maps.event.addDomListener(window, 'load', initialize);
为什么在第一个场景中没有定义moreInfo(),即使我在调用moreInfo()之前定义了它?问题在于您只是定义了
B
函数,而没有调用它
因此,B
中的任何内容都不会执行。我也看不到你调用了一个,因为你没有返回任何东西,所以它是未定义的
您可以使用自执行函数,也可以只调用它
function A() {
function B() {
function C(){
//...
}
// call C()
}
B(); // you call it here, which in turn calls C
}
你能用
C=function()试试吗{…
您是在网页中使用此代码还是其他什么?显示您的实际代码您发布的代码不是您正在运行的代码,在这两种情况下都定义了moreInfo。可能实际代码的拼写或大写字母不同?抱歉,我遗漏了这些部分,因为它们不是问题所在。我编辑了我的问题。