Home > javascript, jquery > 11 More Things I Learned from the jQuery Source

11 More Things I Learned from the jQuery Source

January 19th, 2011

I recently dove back into the jQuery source code to pull out some more JavaScript goodness. If you haven't seen the original 10 Things I Learned from the jQuery Source video, definitely check that out.

But now.. some newness:


youtube link for flash-less HTML5 player

In this video:

Tune in for 29 minutes and soak it up.

Paul Irish javascript, jquery

  1. January 19th, 2011 at 09:17 #1

    Here's a gist of the basic code I was reviewing.

  2. Arian
    January 19th, 2011 at 11:37 #2

    You can use jsFiddle keyboard shortcuts to hide the sidebar ;)

  3. Addy Osmani
    January 19th, 2011 at 11:38 #3

    Enjoyed some of the updated stuff at the jQ summit but awes job at recording an update to this for everyone, Paul.

  4. January 19th, 2011 at 11:47 #4

    @Arian
    Control + Shift + UP — awesome! Thx man.

  5. January 19th, 2011 at 13:14 #5

    Hooray! Excellent bits and ascii dinosaurs as always! Thanks for the mad propz!

    P.S. Dokas is "dough kiss" not that anyone would ever be able to tell :)

  6. January 19th, 2011 at 13:18 #6

    A good watch as always. Hard to find people that have as much fun talking about code as much as you, Paul.

  7. January 19th, 2011 at 13:30 #7

    Dude. JavaScript + Inception references, FTW! :)

    http://sonspring.com/journal/javascript-inception

  8. January 19th, 2011 at 14:37 #8

    Awesome again Paul! I'm really missing yayQuery!

  9. gbakernet
    January 19th, 2011 at 14:59 #9

    Paul,

    I've been wondering why the jQuery Init constructor function is on the jQuery prototype. There's nothing in the API documentation about it and I'm pretty sure jQuery doesn't need it there to work. It could just be locally declared in the IIFE closure.

    You don't see anyone ever doing this

    var elem = $('#one').init('#two');
    // elem.size() === 1
  10. January 19th, 2011 at 18:05 #10

    cssHooks! Them shits are awesome!

  11. January 19th, 2011 at 20:04 #11

    Always have Inception BRAWRMMRRM sound ready: http://inception.davepedu.com/noflash.html

  12. January 19th, 2011 at 23:59 #12

    Great! I've been waiting for more videos from you, informative and entertaining. Is there any other video you've done besides these two and the html5 boilerplate one for nettuts?

    thanks!

  13. January 20th, 2011 at 03:02 #13

    We in Russia do love Dzhavaskript too! We love JQ and you! Waiting for updates!

  14. January 20th, 2011 at 22:44 #14

    Riddle me this Paul, why not use if/else in dataAttr method to assign data? It's so much easier to read and comprehend when scanning through the code. Should these sorts of optimisations not be left to the build stage when something such as closure compiler would take that if/else block and return an optimised ternary statement.

    Verboseness wins everytime for me.

    Also why does jQuery insist on type checking typeof operators when typeof always returns a string?

  15. Kevin
    January 21st, 2011 at 05:47 #15

    Thanks. These are always fun to watch and I scribble notes furiously.

  16. January 22nd, 2011 at 19:25 #16

    Great stuff, I'm usually skeptical with these things, but the:
    - live and static nodelists (and a righteously clever IE sniff)
    - jQuery's new cssHooks, polyfilling support, css3 magic (and their relationship to ascii sunglasses)

    Were some great learning opportunities! Even for javascript experts. Thanks Paul :-)

  17. January 23rd, 2011 at 05:39 #17

    Great stuff, Paul. I'll have to watch this a few more times to get the full impact…. you know, 'cause I'm a wimp. :(

    I do feel that this one could have been better if you stopped halfway through to get a haircut — like you did in the first one. :-)

  18. January 23rd, 2011 at 13:01 #18

    @Ryan
    In this case, I dont think closure compiler would actually rewrite that. I agree the if/else combo MIGHT be friendlier code, but the jQuery core isn't written with readability as a priority.. Speed and size trump readability often. shrug.

    Whatcha mean about typeof? I dont see any `typeof typeof foo === 'string' && `

  19. January 23rd, 2011 at 16:03 #19

    @Paul Irish
    If I run the if/else block through closure compiler[1] it outputs a ternary statement. I'd be interested to see the performance differences between ternary vs if/else.

    I can't find in the video where I saw the typeof checking. But what I'm referring to is the jQuery's use of strict-equal when using the typeof operator e.g. typeof selector === "string", it seems unnecessary when typeof always returns the objects type as a string. Is there a reason or is it just a jQuery code guideline to always use strict-equal?

    [1] http://closure-compiler.appspot.com/home

  20. January 23rd, 2011 at 16:29 #20

    @Ryan
    Supercool! Happy to see that optimization works! Tangentially related, the jquery project just switched to uglify.js from closure compiler.

    Ah I gotcha… the threequals on typeof checks.. Hmmmm.. No there's no reason. I agree it is needless. Just asked the team about the core style guide http://docs.jquery.com/JQuery_Core_Style_Guidelines#Equality and got mostly a consistency answer. The difference is moot in bpth performance and in gzipped filesize.
    So It's probably okay. Personally i prefer threequals ONLY when comparing to falsy values, otherwise just ==

  21. foldip
    January 25th, 2011 at 02:35 #21

    Awesome presentation. I enjoyed both this and the "10 things…" one.
    There is an error I think at 12:50:
    The parentheses you include in the expression change the meaning of it. The precedence table you just showed before doing it says that the && has higher precedence than || and also !== is higher than &&. Your parentheses overwrite and change the natural precedence.

  22. January 28th, 2011 at 23:49 #22

    I'd like to submit an alternative to both the tough-to-grok ternary nesting and the ugly-to-look-at if tower drawn from a more functional mindset.

    var cascade = function(pairs, base){
      for(var i = 0; i < pairs.length; i+=2){
        if(pairs[i]){
          return pairs[i + 1];
        }
      }
     
      return base;
    };
     
    // "pairs" are evaluated with a final base case
    cascade([ data === 'true', true,
              data === 'false', false,
              data === 'null', null,
              !jQuery.isNan( data ), parseFloat(data),
              rbrace.test( data ), jQuery.parseJSON( data ) ],
            data);
  23. January 28th, 2011 at 23:52 #23
  24. January 29th, 2011 at 12:49 #24

    I just LOL'd at the inception comment, and my wife was like, what are you laughing at? Oh, just a really funny screencast about the jQuery source code. She had no response.

  25. Ricardo Vega
    January 31st, 2011 at 17:13 #25

    Very good material. I use often the "switch(true) pattern" when ternaries can get tricky, like:

    switch(true)
    case data === 'true': data = true;
    case data === 'false': data = false;
    … etc

    Regards

  26. February 1st, 2011 at 11:33 #26

    Excellent stuff, fun video! I'm particularly excited about cssHooks. Thanks!!

  27. Tim
    February 2nd, 2011 at 12:15 #27

    bit.ly/jqsource should be updated to 1.5. You promised it would always have the latest… and then you broke that promise.

  28. February 9th, 2011 at 21:18 #28

    Thanks Paul. Without your luminary clown-geekouts learning this stuff would be bleak, indeed. I like the CSS hooks stuff a lot — very much the essence jQuery. You think it'd be possible to use JS to read the CSS itself so that one could use single-selectors _there_ and then benefit from the "object oriented" and cascading power of CSS. So then you could use $(".foo").addClass("bar") you'd benefit from the style children (".bar ul li p") — the p there maybe using "box-shadow". Can you figure that out? More videos!

  29. February 10th, 2011 at 09:49 #29

    @me: _Now_ I get it… I experimented for a couple hours: I didn't quite realize that from JS, for eg., Firefox doesn't even _see_ something like the plain "border-radius" (via document.styleSheets[0].cssRules) —– it only sees "-moz-border-radius".

  30. February 12th, 2011 at 11:18 #30

    What's the difference between cssHooks and "Duck Punching" presented last year? http://paulirish.com/2010/duck-punching-with-jquery/
    Is that the same, but in cleaner way?

  31. February 16th, 2011 at 06:41 #31

    Hey Paul, Great site. I've been spending a lot of time with jquery and I appreciate the your in-depth approach.
    -n

  32. February 23rd, 2011 at 22:08 #32

    This is very cool! Thx

  33. LE
    February 27th, 2011 at 07:47 #33

    You seriously think that contrived ternary expression is beautiful or readable? You in all seriousness argue PRO inline anonymous functions? This is why JavaScript can't have nice things.

  34. March 6th, 2011 at 09:32 #34

    @Paul Irish Latest versions of UglifyJS do some (very limited) type inference and when it can determine that both arguments are the same type, it will turn === into ==. For example typeof foo === "string" will compress to typeof foo == "string" since we know that the result of typeof is always a string.

    But of course, it's better if the programmer takes care of this… I for one rarely use ===.

  35. Dave C
    April 12th, 2011 at 16:29 #35

    Dude
    As with your 10 Things vid, this is a great watch, I really hope you do more like this :)

  36. nitech
    January 10th, 2012 at 03:06 #36

    He Paul. This is cool. Thanks for the funny look into the jQuery source ;-)

    What recorder are you using? Camtasia?

For code blocks, use <pre lang="javascript">. css and html4strict are also accepted.

i left this space here for you to play. <3