将指向对象的方法安全地传递给google.maps.event.addListener [英] Passing a pointer to objects method safely to google.maps.event.addListener

查看:42
本文介绍了将指向对象的方法安全地传递给google.maps.event.addListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将方法指针传递给 google.maps.event.addListener 时,谷歌地图似乎会用 google.maps实例覆盖其 this 引用.Map ,因此,如果方法在内部引用其任何原型成员,则它将变为无用.

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.

长话短说,如果我将 myObj.MyMethod 作为事件回调传递,则在其执行时 MyMethod.this 引用将是 google.maps.Map ,而不是 myObj .

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.

有什么方法可以安全地将方法指针传递给addListener并保持其所有引用完整吗?

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

非常感谢!

推荐答案

使用 addListener()传递方法时,方法传递的对象将在事件发生时丢失,因此方法未定义,您应该调用类似方法

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
})

这篇关于将指向对象的方法安全地传递给google.maps.event.addListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆