9 ms·
Javascript patterns
- puppetmaster3 13y agoGood.
- theonej 13y agothese patterns are cute. I especially like the one where for loops are described, as if the variable declaration practices are important.
- deelowe 13y agoAnd the recommendation to avoid i++ due to "excessive trickiness?" followed by a recommendation to use while(i--) with i initialized somewhere else as a for loop substitute? What the heck? That sounds terrible.
- svachalek 13y agoI thought I might learn something until I saw that exact comment, then decided it was safer to stop reading immediately.
- rnd33 13y agoRegarding function declarations, it considers function declarations to be an antipattern and advises you to instead use function expressions at all times (https://github.com/shichuan/javascript-patterns/blob/master/general-patterns/function-declarations.html https://github.com/shichuan/javascript-patterns/blob/master/...). I'm really skeptical about this. Using function expressions has some quite subtle consequences (hoisting, accidental/unintended closures) which you automatically avoid when using function declarations. Function declarations are also very useful since they allow you to use the function before it is defined (in the code), which mean you can read the code (and use the function name as an abstraction) without having to scroll past all function implementations. To expand a bit on the accidental closures part, a variable bound by a closure is an implicit dependency of that function. By breaking function expressions out as function declarations you can be more explicit about what kind of input that piece of code needs from the outside. I just think overuse (nesting etc.) of closures can lead to some quite messy code, it gets harder to reason about when a variable was actually bound and to what.
- fbgv 13y agoI also doubt its enforcing of good semicolon habits, considering that semicolons are not needed after many a statement in C-like languages, including Javascript. This seems more of enforcing one's personal opinion than anything else, to me.
- ivanca 13y agoIt does bite you if you are not careful, for example: (function(){ var foo = 1; console.log(foo); })() // Semicolon missing! (function(){ var bar = 1; console.log(bar); })() It's ambigous for the parser so this will throw a TypeError: "undefined is not a function". So overall I don't think is a stylistic choose but a safety one.
- rnd33 13y agoSure, but the solution should not be "semicolon everywhere" but rather "semicolon where necessary", where necessary also includes places that can produce ambiguities depending on the code directly following (or after minification). Omitting a semicolon after a function declaration will never produce ambiguous code. What I'm still arguing for is that "enforcing semicolon habits" is not a compelling argument for using function expressions over function declarations. (Perhaps you're talking about semicolons in general, which is something I would rather not get into :)
- ivanca 13y agoEverywhere where it _can_ create an ambiguity (regardless if it does or not); having to look at previous unrelated code to see if you need or not a semicolon is inefficient; better put it everywhere where any adjacent code can create ambiguity. And function declarations can create ambiguous code, because functions are first class citizens on JavaScript it means code like this is valid: var foo = function (bar){ console.log(bar); } but for the lacking semicolon it throws an error if it is followed by: (function(){ var foo = 1; console.log(foo); })()
- deleted 13y ago[deleted]
- bulatb 13y agoPrevious discussion: https://news.ycombinator.com/item?id=5392984 https://news.ycombinator.com/item?id=5392984 This source has some problems.
- dpweb 13y agoWhy call eval like this? (1, eval)('this');
- dcherman 13y agoI'm pretty sure it's due to http://es5.github.io/#x10.4.2 http://es5.github.io/#x10.4.2 section 1A. Calling eval like that causes it to be an indirect eval call which results in it being executed in the global context.
- deleted 13y ago[deleted]
- nairteashop 13y agoI was wondering too. There is a nice answer at SO that expands on what dcherman said: http://stackoverflow.com/a/9107367 http://stackoverflow.com/a/9107367
- deleted 13y ago[deleted]
- kangax 13y agohttp://perfectionkills.com/global-eval-what-are-the-options/#indirect_eval_call_theory http://perfectionkills.com/global-eval-what-are-the-options/...
- simoncion 13y agoThis UI is pretty terrible on somewhat small screens. Here's what it looks like when sized to 1024x768: http://i.imgur.com/X78sHxx.png http://i.imgur.com/X78sHxx.png Note that the links behind the blue translucent rectangle are unclickable.
- kristopolous 13y agoGrey on grey is usually a bad usability move if you expect people to read your content. (For FF use nosquint: https://addons.mozilla.org/en-US/firefox/addon/nosquint/ https://addons.mozilla.org/en-US/firefox/addon/nosquint/ and Chrom* use high contrast: https://chrome.google.com/webstore/detail/high-contrast/ https://chrome.google.com/webstore/detail/high-contrast/) I don't know about others, but there's really no chance I'd be physically able to read those words without things like this. My eyes blur and I feel a physical strain on them - like I'm trying to do some type of eyeball aerobics. I literally can't read more than about a sentence without having to look away a few moments and then return.
- simoncion 13y agoI can read grey on grey just fine, even on my old laptop with its terrible color and worse grey resolution. Moreover, that site isn't grey on grey, it's grey on light cream ( https://en.wikipedia.org/wiki/Cream_(colour) https://en.wikipedia.org/wiki/Cream_(colour) ). If you haven't already, go to a doctor and get your eyes checked out. What you're describing is not typical.
- kristopolous 13y agoI've gotten my eyes checked regularly for 20 years. Nothing medically troubling to report. I'm slightly color blind and wear glasses. I really want to read the content and the substance - but the modern low contrast design has made it difficult for me. Some particularly aggressive sites I print out to read on physical paper. I was able to easily read content for my first 18 or so years on the web until the trend started.
- voltagex_ 13y ago
- avodonosov 13y agoAre you really going to write "var getData = function getDataF () { };" instead of "function getData() {}" for pseudo benefits like * 1. Makes it easier to understand "functions as an object". * 2. It enforces good semicolon habits. * 3. Doesn't have much of the baggage traditionally associated with functions and scope. ?
- rnd33 13y agoAgreed, and what exactly is the baggage traditionally associated with functions and scope that you avoid by using a function expression?
- amckinlay 13y agoAren't these considered "idioms"?
- _mikz 13y agoone page PDF would be great
- alfl23 13y agoIt's not bad, but you barely scratch the surface. Here's one: http://stackoverflow.com/questions/12973706/javascript-collection/12973883#12973883 http://stackoverflow.com/questions/12973706/javascript-colle...
- mattparlane 13y agoVery cool. I'd recommend adding something about callbacks defined in loops, ie: for (var i = 0; i < 10; i++) { $('.thing').click(function() { alert(i); }); }