11 More Things I Learned from the jQuery Source
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:
- pseudo-module patterns with IIFEs (has.js example)
- data()'s new HTML5 powers via massive ternary operator chaining
- how operator precedence is like grade school mathematics.
- live and static nodelists (and a righteously clever IE sniff)
- jQuery's new cssHooks, polyfilling support, css3 magic (and their relationship to ascii sunglasses)
- what
$.fnreally is. omg! - dancing!
Tune in for 29 minutes and soak it up.
Here's a gist of the basic code I was reviewing.
You can use jsFiddle keyboard shortcuts to hide the sidebar ;)
Enjoyed some of the updated stuff at the jQ summit but awes job at recording an update to this for everyone, Paul.
@Arian
Control + Shift + UP — awesome! Thx man.
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 :)
A good watch as always. Hard to find people that have as much fun talking about code as much as you, Paul.
Dude. JavaScript + Inception references, FTW! :)
http://sonspring.com/journal/javascript-inception
Awesome again Paul! I'm really missing yayQuery!
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
cssHooks! Them shits are awesome!
Always have Inception BRAWRMMRRM sound ready: http://inception.davepedu.com/noflash.html
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!
We in Russia do love Dzhavaskript too! We love JQ and you! Waiting for updates!
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?
Thanks. These are always fun to watch and I scribble notes furiously.
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 :-)
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. :-)
@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' && `
@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
@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 ==
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.
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.
unmangled: https://gist.github.com/40f8224e02bf2fc94687
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.
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
Excellent stuff, fun video! I'm particularly excited about cssHooks. Thanks!!
bit.ly/jqsource should be updated to 1.5. You promised it would always have the latest… and then you broke that promise.
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!
@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".
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?
Hey Paul, Great site. I've been spending a lot of time with jquery and I appreciate the your in-depth approach.
-n
This is very cool! Thx
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.
@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 ===.
Dude
As with your 10 Things vid, this is a great watch, I really hope you do more like this :)
He Paul. This is cool. Thanks for the funny look into the jQuery source ;-)
What recorder are you using? Camtasia?