JavaScript 的事件常量?

发布于 2024-12-20 07:46:32 字数 536 浏览 0 评论 0原文

在 Javascript 中,我有这样的代码:

document.addEventListener("mousedown", mouseDownHandler);

有时我可能会胖手指这样的东西:

document.addEventListener("mouzedown", mouseDownHandler);

并且 Javascript 不会认识到我是个白痴,我会很困惑为什么我的处理程序不工作。

Actionscript3 指定事件常量,如下所示:

MouseEvent.MOUSE_DOWN // equiv to the String "mouseDown"

如果我胖手指一个 variableconstant 那么 JS 就会生我的气,我可以真正解决问题快的。 JavaScript 是否有与此类似的东西,或者我是否需要为事件类型创建自己的 JS 伪常量?

In Javascript I have code like:

document.addEventListener("mousedown", mouseDownHandler);

and occasionally I might fat finger something like:

document.addEventListener("mouzedown", mouseDownHandler);

And Javascript won't recognize that I'm a moron and I'll be confused why my handler isn't working.

Actionscript3 specifies event constants like so:

MouseEvent.MOUSE_DOWN // equiv to the String "mouseDown"

If I fat-finger a variable or constant then JS gets mad at me and I can solve the problem real quick. Does JavaScript have anything similar to to this or do I need to make my own JS pseudo-constants for event types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

魂牵梦绕锁你心扉 2024-12-27 07:46:32

没有内置功能可以为您执行此操作。

您可以创建自己的事件列表:

var EventNames = {
  MOUSE_DOWN : "mousedown",
  MOUSE_UP   : "mouseup",
  CLICK      : "click",
  // etc
};

但这并不能保护您免受打字错误的影响,因为 EventNames.MOUZE_DOWN 只会给您 undefined (这可能会被 接受) code>addEventListener() 但 - 显然 - 不做你想做的)。

您可以创建一系列单独的全局变量,然后如果您在“常量”名称上输入错误,浏览器应该会给出错误(并且您的 IDE 或 lint 产品可能会为您检测到输入错误):

var Event_MOUSE_DOWN = "mousedown",
    Event_MOUSE_UP   = "mouseup",
    // etc

但是当然,“全局变量”的成员读完上面的内容,“邪恶”大队已经开始口吐白沫了。您可以通过将所有代码(包括那些“常量”)包装在一个大的立即执行的匿名函数中来在没有全局变量的情况下完成上述操作,或者您可以这样做:

// YNH: "Your Namespace Here" (insert your own namespacing
//                             code as desired)
var YNH_Events = {
   validEvents : ["mouseup","mousedown","click","etc"],

   bindEvent = function(element, eventType, handler, isCustomEvent) {
      if (!isCustomEvent && -1 === this.validEvents.indexOf(eventType))
         throw "Invalid event type requested: " + eventType;

      if (element.addEventListener)
         element.addEventListener(eventType, handler, false);
      else if (element.attachEvent)
         element.attachEvent("on" + eventType, handler);
      else
         element["on" + eventType] = handler;
   }
}

YNH_Events.bindEvent(someElement, "mousedown", function() { });         // OK
YNH_Events.bindEvent(someElement, "customevent", function() { }, true); // OK
YNH_Events.bindEvent(someElement, "mouzedown", function() { });     // Error!

当然,关于使用 Array.indexOf() 适用,即不支持较旧的浏览器,但你可以按照 MDN 的描述来填充它,等等,巴拉,巴拉。

或者你可以像 jQuery 一样定义你自己的 .mousedown(), .click() 等方法,并且只通过这些方法附加事件 - 你肯定会得到一个错误如果您输入错误的方法名称。

There is no built-in feature to do this for you.

You could create your own list of events:

var EventNames = {
  MOUSE_DOWN : "mousedown",
  MOUSE_UP   : "mouseup",
  CLICK      : "click",
  // etc
};

But that doesn't protect you from typos because then EventNames.MOUZE_DOWN will just give you undefined (which will likely be accepted by addEventListener() but - obviously - not do what you want).

You could create a series of individual global variables and then the browser should give an error if you make a typo on the "constant" name (and your IDE or lint product may detect typos for you):

var Event_MOUSE_DOWN = "mousedown",
    Event_MOUSE_UP   = "mouseup",
    // etc

But of course members of the "globals are evil" brigade are already starting to froth at the mouth just from having read the above. You could do the above without globals by wrapping all of your code (including those "constants") in one big immediately-executed anonymous function, or you could instead do something like this:

// YNH: "Your Namespace Here" (insert your own namespacing
//                             code as desired)
var YNH_Events = {
   validEvents : ["mouseup","mousedown","click","etc"],

   bindEvent = function(element, eventType, handler, isCustomEvent) {
      if (!isCustomEvent && -1 === this.validEvents.indexOf(eventType))
         throw "Invalid event type requested: " + eventType;

      if (element.addEventListener)
         element.addEventListener(eventType, handler, false);
      else if (element.attachEvent)
         element.attachEvent("on" + eventType, handler);
      else
         element["on" + eventType] = handler;
   }
}

YNH_Events.bindEvent(someElement, "mousedown", function() { });         // OK
YNH_Events.bindEvent(someElement, "customevent", function() { }, true); // OK
YNH_Events.bindEvent(someElement, "mouzedown", function() { });     // Error!

Of course, the usual caveat about using Array.indexOf() applies, i.e., not supported in older browsers but you can shim it as described by MDN, blah, blah, blah.

Or you could do like jQuery and define your own .mousedown(), .click() etc methods and only attach events through those methods - you'll definitely get an error if you mistype the method name.

美人迟暮 2024-12-27 07:46:32

没有可用于定义 addEventListener 事件的预定义常量/属性。

删除了下面的原始答案(以及修订历史记录):
Event 对象定义了以下常量。使用 Firefox 8.0 和以下代码:

  • alert(Object.keys(Event).map(function(name){return name + "\t" + Event[name]}).join('\n') )

There are no predefined constants/properties which can be used as to define an event for addEventListener.

Stripped original answer below (and in the revision history):
The Event object defined the following constants. Using Firefox 8.0, and the following code:

  • alert(Object.keys(Event).map(function(name){return name + "\t" + Event[name]}).join('\n'))
淡淡绿茶香 2024-12-27 07:46:32

也许使用 jQuery?小问题的大转变,但是 jQuery 为许多 JS 处理程序提供了别名函数,如果你拼写错误的函数名称,你肯定会得到一个错误。

那是,

document.addEventListener("mousedown", mouseDownHandler); // right
$(document).mousedown(mouseDownHandler); // right

document.addEventListener("mouzedown", mouseDownHandler); // wrong, no error
$(document).mouzedown(mouseDownHandler); // wrong, throws error

Maybe use jQuery? Big switch for a small problem, but jQuery has aliased functions for a lot of JS handlers, and if you spell a function name wrong you'll definitely get an error.

That is,

document.addEventListener("mousedown", mouseDownHandler); // right
$(document).mousedown(mouseDownHandler); // right

document.addEventListener("mouzedown", mouseDownHandler); // wrong, no error
$(document).mouzedown(mouseDownHandler); // wrong, throws error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文