Hacker News new | past | comments | ask | show | jobs | submit login
Essential JavaScript design patterns (addyosmani.com)
101 points by cfontes on Feb 28, 2011 | hide | past | favorite | 11 comments



I'm very happy to see that this blog's author finally decided to reduce the over-the-top header he used to have on his site...

only to discover that he completely hijacked the header link to not work with Ctrl or Middle Mouse clicks.

If the author is reading this: the content of your articles (and this e-book) are typically great. I enjoy reading them and encourage others to look into them as well. But failing at the basics on your own website makes me die a little inside.


Hey Pewpewarrows. Thanks for the feedback!. I've been a little held up lately with writing and haven't yet had a chance to strip out all of the old code related to the longer header (but it's definitely on my todo list). Thanks for flagging again!


The author says he's coming out with eBook formats soon.

I've found programming books difficult to read on my small Kindle. This book is just screaming for an interactive iPad format. I think he'd have to implement a Javascript console in the page, rather than relying on Safari's, but this would be more useful than a mobile format, to me.


I swap between my small kindle and a portrait oriented 24" monitor for programming books. I'll read the normal text on the kindle then use PC for the programming bits, which are often tutorials that I need to do anyway.

I'm going to give the Send to Kindle chrome extension a go for this ebook. He mentions ePub and PDF are coming soon, but I don't think the kindle supports ePub and PDF is less than ideal on it.


An on-page console would certainly be an interesting idea and I'll consider it. With respect to other eBook formats, because I manually convert these (so as to avoid tables splitting across pages etc) it may be at least a week off before you see something in place, but I'll do my best!


Just download the HTML file and email it to your kindle. I did it and it works really well.


I fail to see how the following code from the book is an example of a singleton:

01 var mySingleton = function(){

02

03 /* here are my private variables and methods /

04 var privateVariable = 'something private';

05 function showPrivate(){

06 console.log(privateVariable);

07 }

08

09 / public variables and methods (which can access private variables and methods ) */

10 return {

11 publicMethod:function(){

12 showPrivate();

13 },

14 publicVar:'the public can see this!'

15 }

16 }

17

18 var single = mySingleton();

Also, the code is missing semicolons. Missing semicolons is a problem when minifying the code. I would recommend running your example code through JS lint for the next edition of the book.

Still, I've been looking to improve my JS foo and reading through this book may help. I will continue. It is attractively formatted and addresses topics that I want to know more about.


The code above doesn't provide a Singleton implementation, you can test it by using the expression mySingleton() == mySingleton() which should evaluate to true, while it evaluates to false. The problem is that the function returns an object literal but every time the function is called it creates a new one (i.e. {} == {} evaluates to false). You can find a sound definition of a Singleton in JavaScript which makes use of the (quite tricky) Lazy Function Definition here: http://stackoverflow.com/questions/1895635/javascript-single... . EDIT: Took a deeper look at the "book" and the singleton section and I think it somewhat misuses the term Singleton (or it uses in a "broad" sense): the example you reported provides some information hiding but no single instantiation restriction, which it is instead provided by the final example/iteration.


This is an excellent resource. I've been recently looking for a collection of design patterns because I'm currently stuffing all of my js into a huge collection of object literals. While its alot easier to manage than js spaghetti code all over the place, I always felt there was a better way.

I think overall, this compliments both the jQuery Fundamentals book and Eloquent Javascript quite nicely.


Just downloaded the zip... will have a look...big ups for getting it done man!!


Great resource! Nice job!




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

Search: