HackerReposA FIELD GUIDE TO OPEN SOURCE
ProSponsor the shelf♧ 0
← Back to top all-time
chuanxshi
javascript-patterns
HTML2012–2014
chuanxshi / javascript-patterns

javascript-patterns

JavaScript Design Patterns

10Distinct people
10Mentions
0Explicit recommenders
9.3KGitHub stars
REPOSITORY AT A GLANCE

What to know before exploring

What it is

JavaScript Design Patterns

GitHub

Where it is used

No documented product use has been verified yet.

What people are saying

No recommendation phrase was found in the indexed comments.

No classified warning is attached to this page. Read the discussions before treating silence as approval.

Also in these discussions

No other repository is repeatedly linked in the same discussions.

This week on Hacker News

Nov 7–Nov 14 UTC · Direct repository links and separately labeled related project stories. Indexed through 11/14/2026, 6:01:08 PM UTC.

No direct repository links were found in the indexed HN discussions over the past seven days. Older discussions are available below.

Mentions over time

HN mentions by month10 dated mentions

Observed mentions: 2012-01-16 07:44:42 UTC to 2014-08-19 17:50:36 UTC.

0123452012-01-01 00:00:00 UTC to 2012-02-01 00:00:00 UTC (exclusive): 1 mentions; 1 submissions, 0 commentsJan 20122012-02-01 00:00:00 UTC to 2012-03-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-03-01 00:00:00 UTC to 2012-04-01 00:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 comments2012-04-01 00:00:00 UTC to 2012-05-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-05-01 00:00:00 UTC to 2012-06-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-06-01 00:00:00 UTC to 2012-07-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-07-01 00:00:00 UTC to 2012-08-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-08-01 00:00:00 UTC to 2012-09-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-09-01 00:00:00 UTC to 2012-10-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-10-01 00:00:00 UTC to 2012-11-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-11-01 00:00:00 UTC to 2012-12-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2012-12-01 00:00:00 UTC to 2013-01-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-01-01 00:00:00 UTC to 2013-02-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-02-01 00:00:00 UTC to 2013-03-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-03-01 00:00:00 UTC to 2013-04-01 00:00:00 UTC (exclusive): 5 mentions; 0 submissions, 5 comments2013-04-01 00:00:00 UTC to 2013-05-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 commentsApr 20132013-05-01 00:00:00 UTC to 2013-06-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-06-01 00:00:00 UTC to 2013-07-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-07-01 00:00:00 UTC to 2013-08-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-08-01 00:00:00 UTC to 2013-09-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-09-01 00:00:00 UTC to 2013-10-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-10-01 00:00:00 UTC to 2013-11-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-11-01 00:00:00 UTC to 2013-12-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2013-12-01 00:00:00 UTC to 2014-01-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2014-01-01 00:00:00 UTC to 2014-02-01 00:00:00 UTC (exclusive): 2 mentions; 0 submissions, 2 comments2014-02-01 00:00:00 UTC to 2014-03-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2014-03-01 00:00:00 UTC to 2014-04-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2014-04-01 00:00:00 UTC to 2014-05-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2014-05-01 00:00:00 UTC to 2014-06-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2014-06-01 00:00:00 UTC to 2014-07-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2014-07-01 00:00:00 UTC to 2014-08-01 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2014-08-01 00:00:00 UTC to 2014-09-01 00:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 commentsAug 2014

Counts come from the repository’s dated source evidence. Index cutoff: 2026-11-02 08:16:59 UTC.

View mention counts and date ranges
UTC ranges include their start and exclude their end. Select a period to filter the source evidence.
PeriodStart (UTC)End (exclusive, UTC)MentionsSubmissionsComments
Jan 20122012-01-01 00:00:00 UTC2012-02-01 00:00:00 UTC110
Feb 20122012-02-01 00:00:00 UTC2012-03-01 00:00:00 UTC000
Mar 20122012-03-01 00:00:00 UTC2012-04-01 00:00:00 UTC101
Apr 20122012-04-01 00:00:00 UTC2012-05-01 00:00:00 UTC000
May 20122012-05-01 00:00:00 UTC2012-06-01 00:00:00 UTC000
Jun 20122012-06-01 00:00:00 UTC2012-07-01 00:00:00 UTC000
Jul 20122012-07-01 00:00:00 UTC2012-08-01 00:00:00 UTC000
Aug 20122012-08-01 00:00:00 UTC2012-09-01 00:00:00 UTC000
Sep 20122012-09-01 00:00:00 UTC2012-10-01 00:00:00 UTC000
Oct 20122012-10-01 00:00:00 UTC2012-11-01 00:00:00 UTC000
Nov 20122012-11-01 00:00:00 UTC2012-12-01 00:00:00 UTC000
Dec 20122012-12-01 00:00:00 UTC2013-01-01 00:00:00 UTC000
Jan 20132013-01-01 00:00:00 UTC2013-02-01 00:00:00 UTC000
Feb 20132013-02-01 00:00:00 UTC2013-03-01 00:00:00 UTC000
Mar 20132013-03-01 00:00:00 UTC2013-04-01 00:00:00 UTC505
Apr 20132013-04-01 00:00:00 UTC2013-05-01 00:00:00 UTC000
May 20132013-05-01 00:00:00 UTC2013-06-01 00:00:00 UTC000
Jun 20132013-06-01 00:00:00 UTC2013-07-01 00:00:00 UTC000
Jul 20132013-07-01 00:00:00 UTC2013-08-01 00:00:00 UTC000
Aug 20132013-08-01 00:00:00 UTC2013-09-01 00:00:00 UTC000
Sep 20132013-09-01 00:00:00 UTC2013-10-01 00:00:00 UTC000
Oct 20132013-10-01 00:00:00 UTC2013-11-01 00:00:00 UTC000
Nov 20132013-11-01 00:00:00 UTC2013-12-01 00:00:00 UTC000
Dec 20132013-12-01 00:00:00 UTC2014-01-01 00:00:00 UTC000
Jan 20142014-01-01 00:00:00 UTC2014-02-01 00:00:00 UTC202
Feb 20142014-02-01 00:00:00 UTC2014-03-01 00:00:00 UTC000
Mar 20142014-03-01 00:00:00 UTC2014-04-01 00:00:00 UTC000
Apr 20142014-04-01 00:00:00 UTC2014-05-01 00:00:00 UTC000
May 20142014-05-01 00:00:00 UTC2014-06-01 00:00:00 UTC000
Jun 20142014-06-01 00:00:00 UTC2014-07-01 00:00:00 UTC000
Jul 20142014-07-01 00:00:00 UTC2014-08-01 00:00:00 UTC000
Aug 20142014-08-01 00:00:00 UTC2014-09-01 00:00:00 UTC101

What Hacker News says

5 sources
I opened this page: https://github.com/shichuan/javascript-patterns/blob/master/... and saw this pattern used several times: if (value == 0) { return result0; } else if (value == 1) { return result1; } else if (value == 2) { return result2; } I can't think of a coding standard or best practice in any language that would suggest combining return and else like that. Instead it would be: if (value == 0) { return result0; } if (value == 1) { return result1; } if (value == 2) { return result2; } or: if (value == 0) return result0; if (value == 1) return result1; if (value == 2) return result2; Then on this page: https://github.com/shichuan/javascript-patterns/blob/master/... there is this code: // optimization 1 - cache the length of the
Read the rest
array with the use of `max` for (var i = 0, max = myarray.length; i < max; i++) { // do something with myarray[i] } The code is OK, but 'max' is a very poor name. It sounds too much like Math.max, and it isn't the maximum value for the loop. It's one greater than that. If you want to cache the array length like this, then any of these names would be far better than max: either 'length' to match the 'array.length', or 'len' for something shorter, or 'n' if you want it really short. These names all better reflect the meaning of this variable: the length of the array. ('n' stands for 'number', as in the number of elements in the array.) Later on the page it recommends: // preferred 1 var i, myarray = []; for (i = myarray.length; i--;) { // do something with myarray[i] } I'm not a fan of running loops backwards for optimization except where it's really necessary. When I'm reading the code I rarely know whether the backwards loop is simply an optimization, or essential to the algorithm. It adds mystery to the code, and I don't like that. Of course a comment could be added saying "Backwards loop is for optimization only" or "Backwards loop is needed for the algorithm", but I've never seen a backwards loop aficionado do this. A few years ago I worked with someone who hated jQuery and libraries in general and preferred to write his own bare metal DOM loops for everything. And he wrote them all backwards. Later on he warmed up a bit to the idea of using jQuery, after seeing a 20-line bare metal function be reduced to a simple and straightforward selector, but when we started going through the code it was never clear if we needed to add a ":last" to the selector to make it match the backwards loop or not. It's pretty rare that this kind of loop optimization will make much difference, unless the work done in the loop body is truly minimal. Then on this page: https://github.com/shichuan/javascript-patterns/blob/master/... it has this code: // somewhere else in the code // a method was added to all objects if (typeof Object.prototype.clone === 'undefined') { Object.prototype.clone = function () { }; } Now this is part of a discussion about why you should use 'hasOwnProperty()' in a for-in loop to avoid enumerating Object.prototype additions, but there should also be a mention that you should not extend Object.prototype at all, ever, unless you are 100% certain that all the code in your page or project is under your own control and carefully uses hasOwnProperty(). For example, the Google Maps API has for-in loops that do not use hasOwnProperty(). If you extend Object.prototype, then no matter how careful you are in the rest of your own code, you'll have some confusing problems if you later want to add a Google map to your page. (At least the Maps API used to work this way - it's possible that they have started using hasOwnProperty() now - I haven't checked it in a while. But the same problem could occur with any third-party code you may want to use.) OK, well, on this page: https://github.com/shichuan/javascript-patterns/blob/master/... it does mention that you shouldn't extend Object.prototype, but there's no hint as to why you shouldn't. On this page: https://github.com/shichuan/javascript-patterns/blob/master/... switch (inspect_me) { case 0: result = "zero"; break; case 1: result = "one"; break; default: result = "unknown"; } Omitting the break; on the last case in a switch statement is a bad idea. It's too easy to add another case below that and forget the break statement. Better to use break on every case even if it is redundant at the end.
In JavaScript Patterns Collection
I clicked on the Builder Pattern , it linked to a file but it would be more interesting to have a bit of an explanation. That why UML diagrams are usefull. right now https://github.com/shichuan/javascript-patterns/blob/master/... is hard to understand. I would propose a simple exemple for the builder pattern . let's say we have an array of datas : [0,10,200,50] and we want a representation of the data. So the director would use different builders to represent these datas ( a bar chart , a line chart , etc ...). What i find important with design patterns and often lacking in books is real use cases.You often get strange exemples that , may be interesting but you actually can use them in a real application. Usually the builder ,the fly-weight and the chain of reponsability are often explained badly or with horrible exemples. I dream of a bo
Read the rest
ok that does 2 things : - explain each pattern with real exemples and propose a list of real uses cases where the pattern would be usefull - has an extended tutorial at the end of the book the makes the user actually build a full application with all the patterns studied. For instance , a drawing application with different shapes, pens , filters , commands , menus , undo , redo , data serialization , that would use all the main design patterns and make them work together. If such a book exists ( in java , as3 , C# , javascript ,... (just not in C++ or smalltalk ;) ) ) , i would be interested to know about it. Now javascript has some patterns of its own ( module pattern , etc ... ) that's interesting to know them too.
In JavaScript Patterns Collection

MAKE YOURSELF AT HOME

A few shortcuts

/
Search the collection
?
Open this guide
Esc
Close dialogs and navigation

Your saved repositories and interests stay in this browser.