我的指示是:
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 ?)
}
我的指示是:
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 ?)
}
你可以欺骗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
}
您可以使用bind
创建绑定函数(其中的值this
是您的map
对象):
map.canvas.addEventListener("mousemove", mapOnMouseMove.bind(map), false);
但请注意,由于bind
是 ES5 方法,旧浏览器不支持它。上面链接的 MDN 文章提供了一个你可能也想使用的 polyfill。