[英]Javascript adding functions to google.maps.Map
I'm having a little difficulty adding two functions, one which calls the other, to the google.maps.Map object 我在将两个函数(其中一个调用另一个函数)添加到google.maps.Map对象时遇到了一些困难
google.maps.Map.prototype.addMarkerFromJson = function() {
alert("hi");
}
google.maps.Map.prototype.getPictureDataFromJson = function (jsonObj) {
this.addMarkerFromJson();
}
This code throws an error: 此代码引发错误:
Uncaught TypeError: Object #<Object> has no method 'addMarkerFromJson'
which is puzzling in itself, what made me even more confused is when I changed the names: 本身令人感到困惑,更使我困惑的是当我更改名称时:
google.maps.Map.prototype.anotherTestFunction = function() {
alert("hi");
}
google.maps.Map.prototype.aTestFunction = function (jsonObj) {
this.anotherTestFunction();
}
This still doesn't work (the alert doesn't get fired) but it also doesn't raise an error in console? 这仍然不起作用(不会触发警报),但是也不会在控制台中引发错误?
Would love to know exactly what I'm doing wrong here as well as the explaination for the wierdness! 很想知道我在这里做错了什么,以及对怪异的解释!
Edit: As I look at it again, I'm beginning to think that the this
in this.addMarkerFromJson();
编辑:再次查看时,我开始认为
this.addMarkerFromJson();
中的this
this.addMarkerFromJson();
actually refers to the function() definition and not the Map object that I'm attaching it to? 实际引用的是function()定义,而不是我将其附加到的Map对象? In which case how would I go about calling the other function?
在这种情况下,我将如何调用另一个函数?
Answering my own question: 回答我自己的问题:
This was me being stupid! 这是我的愚蠢! I was using mapObject.getPictureDataFromJson() as the success callback for a jquery.get(), so
this
referred to the caller (the get function) rather than the map object. 我使用mapObject.getPictureDataFromJson()作为jquery.get()的成功回调,因此
this
指调用方(get函数)而不是map对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.