1

When passing a method pointer to google.maps.event.addListener, google maps seems to overwrite it's this reference with an instance of google.maps.Map, therefore it renders it useless if the methods references any of its prototype memebers internally.

The long story short, if I pass myObj.MyMethod as an event callback, at the time of its execution MyMethod.this reference will be an instance of google.maps.Map rather then myObj.

Is there any way to safely pass a method pointer to addListener and maintain all of its references intact ?

Many thanks !

1 Answer 1

1

When you passed the method with addListener(), The passed object of method would be lost at event occurred, so as a result method is not defined, you should call the method something like

google.maps.event.addListener('someEvent', function (){
     myObj.MyMethod.call(myObj); //invoked MyMethod with reference myObj

    //myObj.MyMethod.apply(myObj); <= you can use apply method also
})
1
  • That's exactly what I've been looking for, works flawlessly, many thanks ! Mar 16, 2014 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.