全局 Javascript 事件处理对象上下文

发布于 2024-08-21 00:16:24 字数 835 浏览 11 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-08-28 00:16:24

今天,许多人通过显式闭包来完成此操作:

var myobject= new MyObject();
element.onmousemove= function() {
    myobject.onMouseMove();
};

但将来,您将使用 ECMAScript 第五版方法 function.bind 来完成此操作:(

element.onmousemove= myobject.onMouseMove.bind(myobject);

传递给 function.bind 的任何其他参数 在调用时被添加到目标函数的参数列表中。)

在浏览器全部原生支持 function.bind 之前,您可以使用原型和闭包来破解支持。有关示例实现,请参阅此答案的底部。

document.body.scrollLeft

如果您处于 IE Quirks 模式,则只有 document.body。你不想处于怪癖模式。对于标准模式文档类型,它是 document.documentElement。因此,如果您需要支持可能使用任一模式的不同页面,或者由于某种原因您仍然需要支持 IE5(希望不会):

var viewport= document.compatMode==='CSS1Compat'? document.documentElement : document.body;
return {x: ev.clientX+viewport.scrollLeft, y: ev.clientY+viewport.scrollTop};

Today, many people do this with an explicit closure:

var myobject= new MyObject();
element.onmousemove= function() {
    myobject.onMouseMove();
};

But in the future, you'll do it with the ECMAScript Fifth Edition method function.bind:

element.onmousemove= myobject.onMouseMove.bind(myobject);

(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.

document.body.scrollLeft

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's document.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):

var viewport= document.compatMode==='CSS1Compat'? document.documentElement : document.body;
return {x: ev.clientX+viewport.scrollLeft, y: ev.clientY+viewport.scrollTop};
你的心境我的脸 2024-08-28 00:16:24

您需要传递一个包装函数,该函数在正确的上下文中调用您的处理程序。

例如:

addHandler(function(ev) { someObject.onMouseMove(ev); });

You need to pass a wrapper function that calls your handler on the correct context.

For example:

addHandler(function(ev) { someObject.onMouseMove(ev); });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文