简体   繁体   English

javascript的事件常量?

[英]event constants for javascript?

In javascript I have code like 在javascript中,我有类似的代码

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. javascript将无法识别我是一个白痴,我会感到困惑,为什么我的处理程序不起作用。

Actionscript3 specifies event constants like so Actionscript3指定了这样的事件常量

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. 如果我指责variableconstant那么JS会对我生气,我可以快速解决问题。 Does JavaScript have anything similar to to this or do I need to make my own JS pseudo-constants for event types? JavaScript是否与此类似或是否需要为事件类型创建自己的JS伪常量?

There are no predefined constants/properties which can be used as to define an event for addEventListener . 没有预定义的常量/属性可用于为addEventListener定义事件。

Stripped original answer below (and in the revision history ): 删除下面的原始答案(以及修订历史记录中 ):
The Event object defined the following constants. Event对象定义了以下常量。 Using Firefox 8.0, and the following code: 使用Firefox 8.0和以下代码:

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

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). 但这并不能保护你免受拼写错误,因为EventNames.MOUZE_DOWN只会给你undefined (这可能会被addEventListener()接受,但是 - 显然 - 不能做你想要的)。

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

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, ie, not supported in older browsers but you can shim it as described by MDN , blah, blah, blah. 当然,关于使用Array.indexOf()的常见警告是适用的,即在旧版浏览器中不受支持,但你可以按照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. 或者你可以不喜欢jQuery和定义自己的.mousedown() .click()等方法,只有通过这些方法附加的事件-如果你输入了错误的方法名,你一定会得到一个错误。

Maybe use jQuery? 也许使用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. 一个小问题的大转换,但是jQuery为很多JS处理程序提供了别名函数,如果你拼写错误的函数名,你肯定会收到错误。

That is, 那是,

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM