除此之外,我已经删除了这里的每一行代码:
show = foo.show = function () {
alert("test");
};
现在,如果我以这种方式运行 map API,它就可以工作了:
foo.display = function() {
this.elem = document.getElementById('fooBox');
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=show";
document.body.appendChild(script);
};
但是当我尝试通过 callback=foo.show
调用它时,它失败了:
foo.display = function() {
this.elem = document.getElementById('fooBox');
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=foo.show";
document.body.appendChild(script);
};
我可以从 WebKit 检查器得到的唯一错误是 Uncaught TypeError: Cannot call method 'show' of undefined
。
我是 google maps API 的新手,但我认为方法无关紧要?我确定我在某个地方看到过它。我在这里做错了什么?
最佳答案
foo
是否声明为 var
?如果是这样,它就无法在全局范围内访问。添加如下声明:
foo = {};
foo.show = function () {
...
};
...
添加到您的代码中,它很可能会解决问题。来自 WebKit 的响应指示变量 foo
在调用 show()
时不在范围内。
关于javascript - Google maps API,回调是对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7256705/