简体   繁体   中英

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.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(); actually refers to the function() definition and not the Map object that I'm attaching it to? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM