全局 Javascript 事件处理对象上下文
我在 Javascript 的事件处理程序中遇到以下问题。我有一个具有 mousemove
事件处理程序的对象,如下所示:
function MyObject(){ }
function MyObject.prototype = {
currentMousePosition: null,
onMouseMove: function(ev){
this.currentMousePosition = this.getCoordinates(ev);
},
getCoordinates: function(ev){
if (ev.pageX || ev.pageY)
return { x: ev.pageX, y: ev.pageY };
return { x: ev.clientX + document.body.scrollLeft - document.body.clientLeft, y: ev.clientY + document.body.scrollTop - document.body.clientTop };
}
};
我试图解决的问题围绕对象上下文解决。在我的 onMouseMove 函数中,它分配了 currentMousePosition 属性。当然,这是行不通的,因为它是处理 mousemove
事件的静态函数。
我正在寻找一种通过事件处理程序传递对象上下文的技术/方法。我能想到的最好的例子是 Google Maps API 函数 GEvent.bind()
。 使用它,您可以传递带有要在指定事件上触发的函数的对象。我本质上正在寻找同样的东西。
I have the following problem in event handlers in Javascript. I've got an object that has a mousemove
event handler like so:
function MyObject(){ }
function MyObject.prototype = {
currentMousePosition: null,
onMouseMove: function(ev){
this.currentMousePosition = this.getCoordinates(ev);
},
getCoordinates: function(ev){
if (ev.pageX || ev.pageY)
return { x: ev.pageX, y: ev.pageY };
return { x: ev.clientX + document.body.scrollLeft - document.body.clientLeft, y: ev.clientY + document.body.scrollTop - document.body.clientTop };
}
};
The problem I'm trying to solve resolves around object context. Within my onMouseMove
function it assigns the currentMousePosition
property. Naturally this won't work because it's a static function handling the mousemove
event.
What I'm looking for is a technique/method to pass an object context in with my event handler. The best example that I can think of is the Google Maps API function GEvent.bind()
.
With it you can pass the object with the function you want to fire on the specified event. I'm essentially looking for the same thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
今天,许多人通过显式闭包来完成此操作:
但将来,您将使用 ECMAScript 第五版方法
function.bind
来完成此操作:(传递给
function.bind 的任何其他参数
在调用时被添加到目标函数的参数列表中。)在浏览器全部原生支持
function.bind
之前,您可以使用原型和闭包来破解支持。有关示例实现,请参阅此答案的底部。如果您处于 IE Quirks 模式,则只有
document.body
。你不想处于怪癖模式。对于标准模式文档类型,它是document.documentElement
。因此,如果您需要支持可能使用任一模式的不同页面,或者由于某种原因您仍然需要支持 IE5(希望不会):Today, many people do this with an explicit closure:
But in the future, you'll do it with the ECMAScript Fifth Edition method
function.bind
:(Any further arguments passed to
function.bind
are prepended to the argument list of the target function when called.)Until browsers all support
function.bind
natively, you can hack support in yourself using prototypes and closures. See the bottom of this answer for an example implementation.It's only
document.body
if you are in IE Quirks Mode. You don't want to be in Quirks Mode. With a Standards Mode doctype, it'sdocument.documentElement
instead. So if you need to support different pages that might use either of the modes, or you still need to support IE5 for some reason (let's hope not):您需要传递一个包装函数,该函数在正确的上下文中调用您的处理程序。
例如:
You need to pass a wrapper function that calls your handler on the correct context.
For example: