我的指示如下:
map.canvas.addEventListener("mousemove", mapOnMouseMove, false);
function mapOnMouseMove (e) {
// here : this refers to the canvas of the map object
// i want to refer to the map (is there a way ?)
}
答案 0 :(得分:1)
你可以欺骗this
像这样引用map
:
map.canvas.addEventListener("mousemove", canvasOnMouseMove, false);
function canvasOnMouseMove (e) {
mapOnMouseMove.call(map, e);
}
function mapOnMouseMove (e) {
// here : this refers to the map object
}
答案 1 :(得分:0)
您可以使用bind
创建绑定函数(this
的值是您的map
对象):
map.canvas.addEventListener("mousemove", mapOnMouseMove.bind(map), false);
但请注意,由于bind
是ES5方法,因此旧版浏览器不支持此方法。与上面链接的MDN文章提供了您可能也想要使用的填充。