Unobtrusive Javascript Obfuscates Event Handling

前端 未结 5 1683
[愿得一人]
[愿得一人] 2021-02-09 23:08

You know what I liked best about obtrusive javascript? You always knew what it was going to do when you triggered an event.



        
相关标签:
5条回答
  • 2021-02-09 23:25

    One thing: you shouldn't be able to see what will happen in JavaScript by looking at the HTML code. What nuisance is that? HTML is for structure.

    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.

    0 讨论(0)
  • 2021-02-09 23:28

    Check out Visual Event... it's a bookmarklet you can use to expose events on a page.

    0 讨论(0)
  • 2021-02-09 23:34

    To answer your question, try using the Firebug command line. This will let you use JavaScript to quickly grab an element by an ID, and then iterate through its listeners. Often, if used with console.log, you'll even be able to get the function definitions.


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

    Further, if the JS files are properly divided, they will only be addressing one sub-set of the DOM at a time. I have an "ordered checkbox" library, it is in only one JS file which then gets re-used in other projects. 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). If I have a question about my "form validation code", I will look at the formValidation object in formvalidation.js.

    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.

    0 讨论(0)
  • 2021-02-09 23:36

    Calling it "kool-aid" seems unfair. DOM Level 2 events solve specific problems with inline event handling, like the conflicts that always result. 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. It also ensures a better separation of the structure (HTML) and behaviour (JS) layers. 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.

    0 讨论(0)
  • 2021-02-09 23:37

    If you're using jQuery you can take advantage of its advanced event system and inspect the function bodies of event handlers attached:

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

    0 讨论(0)
提交回复
热议问题