简体   繁体   English

不引人注目的Javascript模糊了事件处理

[英]Unobtrusive Javascript Obfuscates Event Handling

You know what I liked best about obtrusive javascript? 你知道我最喜欢的关于突兀的javascript的东西吗? You always knew what it was going to do when you triggered an event. 你总是知道当你触发一个事件时会发生什么。

<a onclick="thisHappens()" />

Now that everybody's drinking the unobtrusive kool-aid it's not so obvious. 既然每个人都在喝着不显眼的kool-aid,那就不那么明显了。 Calls to bind events can happen on any line of any number of javascript file that get included on your page. 绑定事件的调用可以发生在页面上包含的任意数量的javascript文件的任何行上。 This might not be a problem if you're the only developer, or if your team has some kind of convention for binding eventhandlers like always using a certain format of CSS class. 如果您是唯一的开发人员,或者您的团队有一些约束事件处理程序(如始终使用某种格式的CSS类)的约定,那么这可能不是问题。 In the real world though, it makes it hard to understand your code. 但在现实世界中,它很难理解您的代码。

DOM browsers like Firebug seem like they could help, but it's still time consuming to browse all of an element's event handler properties just to find one that executes the code you're looking for. 像Firebug这样的DOM浏览器看起来似乎可以提供帮助,但是浏览所有元素的事件处理程序属性只是为了找到一个执行您正在寻找的代码的浏览器,这仍然很耗时。 Even then it usually just tells you it's an anonymous function() with no line number. 即便如此,它通常只是告诉你它是一个没有行号的匿名函数()。

The technique I've found for discovering what JS code gets executed when events are triggered is to use Safari's Profiling tool which can tell you what JS gets executed in a certain period of time, but that can sometimes be a lot of JS to hunt through. 我发现用于发现事件被触发时JS代码被执行的技术是使用Safari的分析工具,它可以告诉你在一段时间内JS被执行了什么,但有时可以通过很多JS来搜索。

There's got to be a faster way to find out what's happening when I click an element. 当我点击一个元素时,必须有一种更快的方法来找出发生了什么。 Can someone please enlighten me? 有人可以赐教吗?

查看Visual Event ...它是一个可用于在页面上公开事件的书签。

If you're using jQuery you can take advantage of its advanced event system and inspect the function bodies of event handlers attached: 如果您正在使用jQuery,您可以利用其高级事件系统并检查附加的事件处理程序的函数体:

$('body').click(function(){ alert('test' )})

var foo = $('body').data('events');
// you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.

$.each( foo.click, function(i,o) {
    alert(i) // guid of the event
    alert(o) // the function definition of the event handler
});

Or you could implement your own event system. 或者您可以实现自己的事件系统。

To answer your question, try using the Firebug command line. 要回答您的问题,请尝试使用Firebug命令行。 This will let you use JavaScript to quickly grab an element by an ID, and then iterate through its listeners. 这将允许您使用JavaScript通过ID快速获取元素,然后遍历其侦听器。 Often, if used with console.log, you'll even be able to get the function definitions. 通常,如果与console.log一起使用,您甚至可以获得函数定义。


Now, to defend the unobtrusive: 现在,捍卫不引人注目的:

The benefit I find in unobtrusive JavaScript is that it is a lot easier for me to see the DOM for what it is. 我在不引人注目的JavaScript中发现的好处是,我可以更容易地看到它的DOM。 That said, I feel that it is generally bad practice to create anonymous functions (with only few exceptions). 也就是说,我觉得创建匿名函数通常是不好的做法 (只有少数例外)。 (The biggest fault I find with JQuery is actually in their documentation. Anonymous functions can exist in a nether-world where failure does not lead to useful output, yet JQuery has made them standard.) I generally have the policy of only using anonymous functions if I need to use something like bindAsListener from Prototype. (我发现JQuery最大的错误实际上在他们的文档中。匿名函数可以存在于一个虚假世界中,其中失败不会导致有用的输出,但JQuery已经使它们成为标准。)我通常只有使用匿名函数的策略如果我需要使用Prototype中的bindAsListener之类的东西。

Further, if the JS files are properly divided, they will only be addressing one sub-set of the DOM at a time. 此外,如果JS文件被正确划分,它们将一次只寻址DOM的一个子集。 I have an "ordered checkbox" library, it is in only one JS file which then gets re-used in other projects. 我有一个“有序的复选框”库,这是在其他项目然后把它重新使用只有一个JS文件。 I'll also generally have all of the methods of a given sub-library as member methods of either a JSON object or a class and I have one object/class per js file (just as if I were doing everything in a more formalized language). 我通常也会将给定子库的所有方法都作为JSON对象或类的成员方法,并且每个js文件都有一个对象/类(就像我用更正式的语言做所有事情一样) )。 If I have a question about my "form validation code", I will look at the formValidation object in formvalidation.js. 如果我对“表单验证代码”有疑问,我将查看formValidation中的formValidation对象。

At the same time, I'll agree that sometimes things can become obtuse this way, especially when dealing with others. 与此同时,我同意有时事情会变得迟钝,特别是在与他人打交道时。 But disorganized code is disorganized code, and it is impossible to avoid unless you are working by yourself and are a good programmer. 但是混乱的代码是无序的代码,除非你自己工作并且是一个优秀的程序员,否则无法避免。

In the end, though, I would rather deal with using /* */ to comment out most of two or three js files to find misbehaving code, then go through the HTML and remove the onclick attributes. 但最后,我宁愿处理使用/* */注释掉大多数两个或三个js文件来查找行为不当的代码,然后浏览HTML并删除onclick属性。

Calling it "kool-aid" seems unfair. 称之为“kool-aid”似乎不公平。 DOM Level 2 events solve specific problems with inline event handling, like the conflicts that always result. DOM Level 2事件解决了内联事件处理的特定问题,例如总是会产生的冲突。 I don't look back to writing code to use window.onload that has to check whether someone else has assigned it before, and sometimes having it overriden by accident or out of sloppiness. 我不回头编写代码来使用window.onload ,它必须检查是否有其他人之前已经分配过它,有时候它会被意外或者不合时宜地覆盖。 It also ensures a better separation of the structure (HTML) and behaviour (JS) layers. 它还确保更好地分离结构(HTML)和行为(JS)层。 All in all, it's a good thing. 总而言之,这是一件好事。

Regarding debugging, I don't think there's any way to solve the event handlers being anonymous functions, other than nagging the authors to use named functions where possible. 关于调试,我认为没有办法解决事件处理程序是匿名函数,除了唠叨作者在可能的情况下使用命名函数。 If you can, tell them that it produces more meaningful call stacks and makes the code more maintainable. 如果可以,告诉他们它会产生更有意义的调用堆栈并使代码更易于维护。

One thing: you shouldn't be able to see what will happen in JavaScript by looking at the HTML code. 有一点:您不应该通过查看HTML代码来了解JavaScript中会发生什么。 What nuisance is that? 这有什么滋扰? HTML is for structure. HTML用于结构。

If you want to check what events are bound to certain elements, there's a bookmarklet called visual event for now, and firebug 1.6 (IIRC) will have some sort of event inspector. 如果你想检查哪些事件绑定到某些元素,那么现在有一个名为visual event的bookmarklet,而firebug 1.6(IIRC)将有一些事件检查器。

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

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