Hacker News new | comments | ask | show | jobs | submit login
John Resig: Secrets of the JavaScript Ninja Update (ejohn.org)
214 points by shill on July 11, 2012 | hide | past | web | favorite | 64 comments



I really admire Resig, and this post contains a few gems:

1. Knew about PPK[0], didn't know about JZ[1]

2. Feature detection came about from writing this book! Which generalizes too: writing is a great way to innovate (something pg has said, too).

3. A nice little jab at GRRM, who thoroughly deserves it.

4. An exciting glimpse into the future of computer science pedagogy by way of JavaScript in the browser. (And I can't help but wonder if this is an oblique response to the recent Alan Kay Dr. Dobbs interview, where Kay asserts, "The Web, in comparison, is a joke. The Web was done by amateurs."[2])

5. Points out that even the best of us can get stuck in a procrastination loop.

6. Resig is going to finish a JavaScript book [3]

[0] http://www.quirksmode.org/

[1] http://perfectionkills.com/

[2] http://www.drdobbs.com/architecture-and-design/interview-wit...

[3] http://jsninja.com/


Regarding #2, regulars on comp.lang.javascript have been saying this since at least 2008, notably in critiques of jQuery. Resig even posted to some of these threads defending jQuery's misbehavior (though I don't recall if he defended browser sniffing). I find it very surprising that it took the act of writing a book to realize this fundamental design flaw. And for those who will want to say "oh, that's just because David Mark is so grating," quite a few mild-mannered regulars reached the same conclusion, so it's not as though there was one firebrand suggesting this.


Interestingly, jQuery was the first major library to switch to a feature detection model. Looking around it appears that a number of other libraries still use browser sniffing (such as MooTools, Prototype, and Dojo). The reason why I switched jQuery over to that model is largely due to my personal research into the matter during the process of writing Secrets of the JavaScript Ninja (largely as a desire to future-proof jQuery against upcoming bug fixes by major browsers).

It's rather amusing to see a case where I, unfortunately, fed the trolls trotted out as an example of future decision making. In this case the decision behind this was my own and largely done through my personal examination of the jQuery code base.


Obviously your decision to implement feature detection in jQuery was your own, but you're doing a disservice to the comp.lang.javascript regulars who laid this out for you. Describing David Mark as a troll is also unfair: I concede he has been rude to you and others, but his knowledge was and is extensive and, like it or not, was something that at the time you could learn from.


Also, here's Resig's take on feature detection circa '07:

"Yawwwwnnnnn.... this is, quite possibly, the most inane set of ramblings set up by someone who has obviously never created a JavaScript library of any kind, nor has used JavaScript code in any sort of production environment."

https://groups.google.com/d/msg/comp.lang.javascript/QVlJ0bz...

    >> Do you have any idea how many agents jQuery will identify as IE?
    >The ones we care about?
I wonder why jQuery eventually turned to feature testing...


I think that post has less to do with my opinions of feature testing and more to do with the environment presented by that usergroup.

As to why jQuery moved to feature testing, we discussed that in the jQuery 1.3 release notes: http://docs.jquery.com/Release:jQuery_1.3


Well, you did respond to the code critiques after all, so that makes it about jQuery's source.

But I do agree that your opinion was biased by the behavior of the people participating that discussion (if that's what you meant). Nonetheless, IMO, a community leader should be able to deal with criticism, and I don't recall reading personal attacks.

As of your link, I was being sarcastic.


I wish I could say that I haven't been personally attacked by them, unfortunately it has been rather extensive and ongoing. Thankfully I've been able to avoid most of it by not visiting clj since 2008-ish and blocking choice Twitter users. Sometimes is trickles back up to me from people letting me know what's going on.

I'm 100% fine with code criticism - I think you'll find no JS library more open to it than the jQuery project, we've shifted the code base many times over the years making large quality/performance/API improvements based upon criticism. That being said I have no interest in talking with people - valid code criticism or not - if they're personally attacking me as well.


So you do agree that browser sniffing is a short-sighted method to test for browser capabities and that clj has been preaching on it since 07?

I'm not judging or anything, but sticking around on clj and trying to argument could have been very beneficial for both jQuery anc clj itself. There are ways of ignoring people in usenet, you know.

Also, Juriy, whom you cite in the book's preface, was a regular there long before his website really took off. That means you didn't dislike every member ;)


He just wrote a post about being attacked, and you attack him? (Perhaps I'm misreading your tone, but it seems to be really aggressive)


I'm not attacking anyone (at least not on purpose), I'm just trying to make a point here.

Resig's actions towards clj effectively turned his whole userbase against everyone and everything that was written in that newsgroup. Thousands of unexperienced js developers shunning great advice just because it came from someone Resig had a beef.

I do believe that it had a negative effect on the javascript industry for a few years.


Yeah, JavaScript really hasn't gone anywhere since Resig left clj in 2008.


That's obviously not what I have said.


Let's just stifle real discussion so we can be sure no aggressive statements are made and no one's feelings get hurt.

The nannying influence of people like you is poisonous to a community. Your own comment is a veiled but difficult-to-analyse attack. Your weak observation asks us cow to your vision so that we will all respect one another and feel good about ourselves. You do not respect the technical aspect of this community which would value truth and quality discussion over feeling.


Or perhaps, lets behave like adults and have adult conversations with other adults.


Lets be 'adults', huh? I'm not surprised that your response would have emotional and vague language. What does that mean 'lets behave like adults'?


What's the story with that David Mark guy? Based on the parent to that post (https://groups.google.com/d/msg/comp.lang.javascript/QVlJ0bz...) he seems to be mentally ill or something - there's about three pages of stream-of-consciousness ranting.

Or maybe I just haven't been on usenet news for a while...


   it took the act of writing a book to realize this fundamental design flaw.
Not cool. Most everyone is too close to their own work.

Writers need editors. Artists need critics. Devs need QA/test.

Fortunately, letting code rest for 6+ months is a pretty good strategy for getting a "new mind".

So is explaining your work to someone else, such as writing about it.


Exactly. Eventually everyone turned to feature testing and it became common sense in clj.

comp.lang.javascript's FAQ on browser-sniffing: http://jibbering.com/faq/notes/detect-browser/


I think you and I are saying the same thing, but to be clear, this was "common sense" on CLJ long before mainstream libraries adopted the practice.


Yes, that's what I was trying to say.


What is GRRM?


Sorry, should have added a footnote for him: http://georgerrmartin.com/ There's also a wikipedia article, of course.

He wrote the first 3 books of his 7 book series at a normal pace, about every 18 months, but then slowed to a crawl for book 4 - something like a 5 year delay. Writer's block happens, but what makes people angry is that he continues to have an extensive "extra-curricular" schedule involving workshops, cons, and travel. And he's told his fans "I don't owe you anything," and has shown not only not-shame, but active antipathy to those who have the temerity to call him out on his irresponsibility. What's worse, is that his bad behavior is being rewarded with new interest thanks to the HBO miniseries of the books.

I mean, manipulating financial markets for gain, losing your bets, and taking taxpayer money to give yourself a bonus is one thing, but leaving fantasy readers in a lurch is quite another level of villainy.


So where's your chunky fantasy trilogy then, if they're so easy to write?

Edit, since I'm being downvoted, and that was written in a hurry on the train: I wrote a book too. It was really fucking hard, and took a long time, but I'm ultimately happy with the way it came out, and that I took the time to do it properly. The reward for that care and hard work seems to be not "great book, thanks!" but people bitching about how long it took to write.

Hence, I now have a very low tolerance for people complaining about other people's work and being whiny and entitled.


Neil Gaiman says it best. George R.R. Martin is not your bitch.

http://journal.neilgaiman.com/2009/05/entitlement-issues.htm...


Gosh, I don't recall saying that he was my bitch. Really. Can you point out where I did?

Writing code (which is what most of us do here, I think) is a lot like writing books, so it's not like I don't understand blockage or the whole "having a life" thing. Heck, I could understand him never writing another word again if his muse died. That's human and understandable. The thing that draws my ire is not the lack of productivity, but the attitude he has about it. (Which is less that GRRM is my bitch, and more that he considers me his.)


> Gosh, I don't recall saying that he was my bitch. Really. Can you point out where I did?

>> He wrote the first 3 books of his 7 book series at a normal pace, about every 18 months, but then slowed to a crawl for book 4 - something like a 5 year delay. Writer's block happens, but what makes people angry is that he continues to have an extensive "extra-curricular" schedule involving workshops, cons, and travel. And he's told his fans "I don't owe you anything," and has shown not only not-shame, but active antipathy to those who have the temerity to call him out on his irresponsibility.

You didn't call him a bitch, but I do get a feeling that you feel he should drop his extra-curricular activities until he is done with the book, he owes his fans, should show shame for not completing the book etc.

He is a writer who writes books for living. You bought his book and that entitles you to the book and nothing else. Extra-curricular, owing something to you and shame for not following up aren't included with the book.

> (Which is less that GRRM is my bitch, and more that he considers me his.)

Yeah. If he says "I will write when I feel like writing" is totally considering you his bitch, right? Gosh, the sense of entitlement.


> Gosh, I don't recall saying that he was my bitch. Really. Can you point out where I did?

Sure, here you go:

> has shown not only not-shame, but active antipathy to those who have the temerity to call him out on his irresponsibility.


Actually, that's calling him out precisely for his mistake - not admitting his irresponsibility, and shamelessly attacking those who've pointed it out. Can you think of any other author who's done this? Gaiman's piece is riddled with holes, and is a rather obvious partisan piece - writers unite and all that. Intriguingly, even within that piece, Gaiman reveals that he felt bad about blowing his deadline, something that GRRM not only hasn't revealed, but has attacked people for wondering why not.

There is actually a good reason for readers to prefer writers who demonstrate some sense of responsibility for finishing what they start. Writers that do not demonstrate a sense of responsibility do not seem to actually finish their work. And whatever Gaiman or Martin say, there is a (at least weak) contract between author and reader that the writer will finish a series that he begins. If the author does not want to make that contract, then he is free to write a stand-alone novel, and then write sequels as he likes.


> that's calling him out precisely for his mistake - not admitting his irresponsibility

By deciding that him taking time writing his book and not living as a hermit is "a mistake" and "irresponsible" you — and others in your situation — are very much calling him your bitch.

> Can you think of any other author who's done this?

Erm... yeah? http://en.wikipedia.org/wiki/Robert_Jordan

Here's a second one for the same price: http://en.wikipedia.org/wiki/Donald_Knuth

And a third one because why not: http://en.wikipedia.org/wiki/Kazushi_Hagiwara

> something that GRRM not only hasn't revealed

At this point, does GRRM even have deadlines?

> And whatever Gaiman or Martin say, there is a (at least weak) contract between author and reader that the writer will finish a series that he begins.

No, there is not.

> If the author does not want to make that contract, then he is free to write a stand-alone novel, and then write sequels as he likes.

That doesn't make a lick of sense, the author could fucking be hit by a bus tomorrow and you as a reader would be left in the cold just the same, except without the ability to find shit to bitch about.

Here's an idea if you don't like waiting and the risk that includes: only read finished series. There, you're done, the advice is free and have a nice day.


That was a good read - thanks :)


That's a whole bucket of meh, it took Knuth 32 year between TAOCP Volume 3 and the beginning of the volume 4 facsimile.

I can't help but cheer for GRRM, even though I'm waiting for the next book to be published.


How dare he!


George R. R. Martin. Wrote the Game of Thrones novel series. He's known for taking his time with writing his novels and instead focuses on other things, like ranting about the NY Jets. His last book, "A Dance of Dragons" took about 5+ years to finish and get to publication. It was published in 2011, the last novel before that was published in 2005.


George R. R. Martin is the author of the 'A Song of Ice and Fire' series, otherwise known as Game of Thrones--the title of the first book and the HBO adaptation.

He has gone as long as six years between book releases in the series.


It's been so long I completely forgot I bought the book!

The best way to get a project done is to

   1. need the money
   2. not get paid until you finish
If either of those elements are missing, expect delays ;-)


I've written a few short-ebook-like technical posts and it still surprises me the number of people who ask for when the PDF format is coming... I get that not everyone can have access to a web browser at every occasion they have for reading...but the web provides a much better canvas for illustrating technical concepts, especially with the ability to create interactive bits with JS. Save the couch reading for leisure/non-technical reading, and read technical pieces when you're on the computer (and have ready access to Google/StackOverflow).

On a selfish note, writing HTML seems to be much easier than producing a properly formatted PDF page. So I enjoy technical writing, but not enough to go through the hoops of for-paper (or paper like e-books) publishing.


I was actually thinking about this earlier. Why is pdf so hard to deal with? And why is around in the first place? I can imagine a future where html5/js combo is enough to convert a set of html pages to a nice offline-supported readable format.

With that said, I usually just go to the print page of some links and read. You will be surprised how many sites inadvertently give the best reading experience through their printer page (NYT for example)


And why is around in the first place? I can imagine a future where html5/js combo is enough to convert a set of html pages to a nice offline-supported readable format.

Same deal with MP3 vs other formats. PDF is a sort of gold standard in being able to precisely lay out text and know it'll render mostly right on most devices (even if the format/layout is wrong for those devices).

HTML5 and EPUB are getting there but vary so much between different platforms that till now (and EPUB 3 is changing this) the quality of books in those formats has been hideous production wise. Compare the typical EPUB book against the typical PDF book in terms of whitespace, layout, fonts, and how the author and publisher intended it to look.


PDF exists because the format allows a UA to render any given page in a document without having to render the whole document. With PostScript you had to run the entire program to accurately know what would be on a particular page.


> And why is around in the first place?

PDFs predate both CSS and the legion of webdevs we have available now. It's effectively vestigial.

However, PDFs do allow for digital signing, which is not a feature webpages can have AFAIK.


I don't know if it's hard as I've never tried. But as I've tried a lot of ways to print websites as PDFs, through various third-party services, I'm assuming it's not at all straightforward...

I think the "hard" part is properly paginating things? And I guess generating table of contents, inner-book-links, things that my blogging platform (Octopress) handles in a way that I've been used to as a Rails templater.


We can import blogs and generate ebooks (PDF, EPUB and MOBI) at Leanpub. The idea is that you should self-publish as you write, and that starting with a blog is a great way to start. We generate a table of contents, support crosslinks, etc. It may be what you want...


There are a number of things that don't quite transfer like headers and footers, keeping blocks together and of course paper doesn't have scroll bars.


Did you give a try to wkhtmltopdf? http://code.google.com/p/wkhtmltopdf/


What a fantastic tool. Thanks!


If you're interested in HTML->PDF without the hoop jumping have a look at princeXML. It's incredibly good.


I haven't read the book, but this praise of with statements struck me as odd: "The portions of this book that cover features that are relatively un-changing, such as code evaluation, with statements, and timers are continually being used in interesting ways." There also seems to be even a whole chapter in the book dedicated to with statements. What are those interesting and great applications that justify this feature, being considered harmful[0] and even removed from the language in strict mode of ES5?

[0] http://www.yuiblog.com/blog/2006/04/11/with-statement-consid...


Every where you see the "with statements are harmful"/"eval is harmful" drum being hit, you see Douglas Crockford. He's a great guy, and I really respect his opinion, but I continue to feel that these are legitimately useful aspects of the JavaScript language -- especially if we want to consider JavaScript as the "assembly language" upon which other languages are built.

For with statements in particular, templating and scoped evaluation (I provide an example from the Firebug console in the book) are probably the two areas in which I've seen it most successfully used.

For code evaluation I only have to direct you to the creation of Coffeescript (an entirely evaluated language) whose innovations have directly influenced new versions of the ECMAScript language.

They have their place and when used sparingly, but effectively, the can be formidable parts of a web developer's arsenal.


I agree with what you're saying, but I'd also be remiss if I didn't quibble with this bit:

    > For code evaluation I only have to direct you to the creation 
    > of Coffeescript (an entirely evaluated language) whose 
    > innovations have directly influenced new versions of the 
    > ECMAScript language.
The standard way to use CoffeeScript is to compile it to JavaScript first, and then deploy and run with that. There's zero "eval()" involved. Or rather, doing direct "eval()" of CoffeeScript is possible, and on occasion fun, but is discouraged for all the usual reasons.


I agree that code evaluation is a valuable feature in the language. But to be accurate, I wouldn't call CoffeeScript an entirely evaluated language, since it's mainly meant to be used by pre-compiling it to JS. (From CoffeeScript website: "While it's not recommended for serious use, CoffeeScripts may be included directly within the browser") Anyway the ability to eval code on the browser is awesome for trying it out or for building an IDE.


The problem is that "with" statements basically disable lots of JIT optimizations in actual implementations, with browsers either falling back to a much slower baseline jit or running the function containing "with" entirely in an interpreter.

So they _can_ be used effectively if one really knows what one is doing, but they're a bit of a footgun.


There are some peculiarities with the `with` statements, but mainly it is just dog-slow compared to a standard lookup. Last time I checked it was 200-300 times slower to use the `with` statement rather than just doing an `obj.key` lookup.

If nothing else, surely it should be avoided in production purely for performance reasons.


I don't know about actual uses, but I was thinking about a very hypothetical one: pattern-matching. I like my languages to have some sort of pattern matching; however, to make it work properly you have to be able to add variables to the local scope programmatically. With statements are JavaScript's way of letting you access the local scope in a dynamic but limited way.

You can imagine code like:

    function () {
      with (match("Foo a b")) { return a + b }
      with (match("Bar a b")) { return a - b }
    } 
This would let you have basic pattern matching. Of course, this would also necessitate adding constructors (in the pattern-matching sense) to the language, so it would be fairly complicated. A simple option would be to just support lists and objects:

    function () {
      with (match("[a, b]")) { return a + b }
      with (match("[a]")) { return a }
      with (match("[]")) { return 0 }
    }
You could imagine this syntax being streamlined a bit.

Basically, the with statement gives you a bit of access to the variable scope which is very useful in adding features to the language. It isn't quite as flexible as some alternatives (e.g. macros) but it can still be useful.


Michael Bolin (author of Closure, definitive guide) gave a presentation advocating the use of "with" for a particular problem: http://bolinfest.com/talks/2011/jsconf.us/#1


How is the proposed usage (slide #8) different than the bad usage on slide #4 ("shorthand for setting multiple properties on an object")? The code for sharing module scope works fine as long as you stick with "a.i" instead of just "i" (i.e., are not lazy).

As mentioned elsewhere, using `with` is very slow: http://jsperf.com/with-versus-without


John is a really great guy and has done (at least for me) more to advance the knowledge of javascript than any other person I know.

Very fitting that he's now at Khan Academy.

I've personally bought the jsninja ebook for friends and myself because I thought it was a great read and wanted to contribute to this kind of effort.


I was looking forward to learning how to do a javascript ninja update, which I assumed might mean some kind of "reload the latest version of the JS while the user is on the page".


While it's disappointing that it's taken this long for the book to get finished, the publisher Manning has provided downloadable drafts throughout the whole process - a great feature. Thanks to that I've had access to the book (such as it's been) for pretty much the entire time, and that's what pushed me to buy it when I did.

Can't wait till my paper copy ships - guess I better make sure my address is accurate still :)


Really respect that he chose to prioritise his personal life. Reminds me of the lesson I learnt from Herbert and Harry as a child. http://www.penguin.com.au/products/9780140567830/herbert-har...


Congratulations! Writing a book is insanely hard.


Is a "JavaScript ninja" conceptually similar to a "my little pony samurai"?


Too bad I cancelled my order years ago...


What? The book is going to be great.




Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: