TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Airbnb JavaScript Style Guide

303 pointsby webnannersalmost 10 years ago

35 comments

brwnllalmost 10 years ago
The best part of this guide is that they included a eslint and jslint rules file.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript&#x2F;tree&#x2F;master&#x2F;linters" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript&#x2F;tree&#x2F;master&#x2F;linters</a><p>My team adopted this style guide and was easily able to add it as a eslint step to our existing gulp files and run it automatically. This let us use the guide without making everyone memorize the syntax differences between their personal style first.
eliseealmost 10 years ago
Regarding using single quotes for strings (<a href="https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript#6.1" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript#6.1</a>), I found it interesting that it&#x27;s one of the rare sections where there&#x27;s no rationale offered.<p>I guess it&#x27;s just a stylistic choice in the end, but when we set up our own internal&#x2F;informal style guide, my teammate and I spent a little while trying to come up with a justification for single vs double quotes. We ended up choosing double quotes based on the fact that JSON already made that decision for us: it requires strings be double-quoted.<p>(Although again, it&#x27;s far from an important matter, as long as you&#x27;re consistent), anybody has interesting rationales to share in favor of single quotes?
评论 #9824426 未加载
评论 #9823713 未加载
评论 #9823716 未加载
评论 #9823656 未加载
评论 #9823700 未加载
评论 #9824048 未加载
评论 #9826034 未加载
评论 #9823995 未加载
评论 #9824618 未加载
评论 #9825605 未加载
评论 #9824794 未加载
评论 #9823655 未加载
评论 #9823643 未加载
评论 #9823701 未加载
svieiraalmost 10 years ago
The fact that `typeof` is no longer safe [1][2] is news to me - it feels like they put in air bags and removed the seat belts.<p><pre><code> [1]: https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript#14.1 [2]: http:&#x2F;&#x2F;es-discourse.com&#x2F;t&#x2F;why-typeof-is-no-longer-safe&#x2F;15</code></pre>
评论 #9823494 未加载
评论 #9826006 未加载
评论 #9823438 未加载
tehwebguyalmost 10 years ago
I didn&#x27;t know you could do this!<p><pre><code> &#x2F;&#x2F; good const obj = { id: 5, name: &#x27;San Francisco&#x27;, [getKey(&#x27;enabled&#x27;)]: true, };</code></pre>
评论 #9823437 未加载
评论 #9827095 未加载
BinaryIdiotalmost 10 years ago
Looks like a lot of good stuff but it&#x27;s incredibly verbose and dense. It&#x27;s important to adhere to standards but I&#x27;m not entirely convinced this doesn&#x27;t end up being counter-productive. But as long as it&#x27;s a guide and not a 100% &quot;you must follow every little thing&quot; and you can change things then maybe it&#x27;s not so bad.<p>Still hard to get used to so many using ES6 already. I&#x27;m still not a big fan of transpiling but some days I feel like I&#x27;m the only one.
评论 #9823574 未加载
z3t4almost 10 years ago
I love these kind of style guides. But I also love to prove them wrong.<p>Unless you &quot;use strict&quot;, it&#x27;s better to put var in-front of every variable if you put them on separate lines.<p><pre><code> var foo = 1, bar = 2 baz = 3 </code></pre> vs<p><pre><code> var foo = 1; var bar = 2; var baz = 3; </code></pre> Forgetting a comma or semicolon in the first example might lead to silent bugs that will take hours to find.
评论 #9825993 未加载
评论 #9826742 未加载
eltacoalmost 10 years ago
You can use the airbnb preset with JSCS (javascript code style checker) [1].<p>Also there&#x27;s an autofix feature for most of the whitespace rules (`jscs src --preset=airbnb --fix`) so you won&#x27;t have to fix everything manually.<p>[1]: <a href="http:&#x2F;&#x2F;jscs.info&#x2F;overview.html" rel="nofollow">http:&#x2F;&#x2F;jscs.info&#x2F;overview.html</a>
cnpalmost 10 years ago
This is the ES6 guide I send around to those who are trying to get up to speed with JavaScript -- its fantastic.
z3t4almost 10 years ago
About referencing &quot;this&quot;: It can be very useful when writing object constructors, if you always reference an object with the constructor&#x27;s name.<p><pre><code> function Foo() { var foo = this; foo.bar = 1; foo.az = 2; } var foo = new Foo(); foo.bar = foo.bar + 5; </code></pre> Then it will be super easy to rename say foo.bar to something else. It&#x27;s also self &quot;documented&quot;.
heydanreevesalmost 10 years ago
There&#x27;s also a jscs default for this styleguide.<p><pre><code> { &quot;preset&quot;: &quot;airbnb&quot; }</code></pre>
ewalk153almost 10 years ago
There is also a link on the page to browser compat style guide of es5 <a href="https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript&#x2F;tree&#x2F;master&#x2F;es5" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript&#x2F;tree&#x2F;master&#x2F;es5</a>
neebzalmost 10 years ago
Does anybody have experience integrating style guides in your existing code base?<p>Our old code base doesn&#x27;t follow any style guide. After adding a style guide it requires us to go back and fix all our old files which is time consuming + kinda messes up with the git history.
评论 #9825586 未加载
liviualmost 10 years ago
<i>&quot;Recommend using .jsx extension for React components, rather than .js&quot;</i><p>I like .js over .jsx because I can require&#x2F;import without explicit extension.<p><pre><code> import Foo from &#x27;.&#x2F;Foo&#x27;; vs import Foo from &#x27;.&#x2F;Foo.jsx&#x27;;</code></pre>
评论 #9824109 未加载
评论 #9824126 未加载
orthoganolalmost 10 years ago
Why are they worried about working by reference on numbers?<p><pre><code> &#x2F;&#x2F; bad var a = 1; var b = 2; &#x2F;&#x2F; good const a = 1; const b = 2;</code></pre>
评论 #9823443 未加载
评论 #9823166 未加载
TheAceOfHeartsalmost 10 years ago
As an alternative style guide, consider giving standard [0] a try. The hook is: &quot;No decisions to make. No .eslintrc, .jshintrc, or .jscsrc files to manage. It just works.&quot; You don&#x27;t have to configure anything, you just run it on your project and it&#x27;ll tell you what to change.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;feross&#x2F;standard" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;feross&#x2F;standard</a>
seniorsassycatalmost 10 years ago
I&#x27;d like to see the justifications for some of these, particularly 3.5 which I see as only obscuring that the current context is a function.
评论 #9823352 未加载
评论 #9823269 未加载
bshimminalmost 10 years ago
<p><pre><code> 3.3 Use readable synonyms in place of reserved words. &#x2F;&#x2F; bad const superman = { class: &#x27;alien&#x27;, }; &#x2F;&#x2F; bad const superman = { klass: &#x27;alien&#x27;, }; </code></pre> What is unreadable about &quot;klass&quot;? Rails, for example, uses &quot;klass&quot; and it&#x27;s never been hard for me (or, I suspect, anyone) to understand.
评论 #9824243 未加载
评论 #9824266 未加载
dkrvtalmost 10 years ago
I&#x27;m curious as I haven&#x27;t found a good coding style guide about object inheritance in ES5.<p>I usually write this even though it&#x27;s a bit verbose:<p><pre><code> function Child() { Parent.call(this); } Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; </code></pre> Any opinion on this or link to a good guide?
评论 #9823992 未加载
ilakshalmost 10 years ago
So basically they could have just written &quot;use all of the new features except these three things..&quot;.
BurningFrogalmost 10 years ago
Maybe this is the IPA talking, but I don&#x27;t think I ever want to work at a company with a style guide again.<p>This is no worse that others I&#x27;ve seen, but they all codify what some group found useful at some point in time, and then that becomes Company Policy set in stone for the rest of time.
评论 #9823562 未加载
评论 #9823675 未加载
评论 #9829036 未加载
评论 #9823419 未加载
lewisjoealmost 10 years ago
<a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;21545687&#x2F;javascript-vs-new-object-performance" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;21545687&#x2F;javascript-vs-ne...</a><p>-explains why {} is better than new Object().
naileralmost 10 years ago
Alternatively, idiomatic.js <a href="https:&#x2F;&#x2F;github.com&#x2F;rwaldron&#x2F;idiomatic.js&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rwaldron&#x2F;idiomatic.js&#x2F;</a>, an older publication with more people contributing to it.
manojldsalmost 10 years ago
ES5 guide - <a href="https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript&#x2F;blob&#x2F;master&#x2F;es5" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;airbnb&#x2F;javascript&#x2F;blob&#x2F;master&#x2F;es5</a>
keithwhoralmost 10 years ago
Love this guide. Adhere to it as tightly as possible for most new projects, saves a lot of mental overhead. Use the ES5 version for team frontend projects. :)
评论 #9823214 未加载
nodesocketalmost 10 years ago
Very nice work, but not sure that I&#x27;m onboard with switching `var` to `const | let`. Is the only difference between `var` and `let` scoping?
评论 #9824425 未加载
da4c30ffalmost 10 years ago
<p><pre><code> 3.5 Use object method shorthand. </code></pre> I disagree. With anonymous objects it breaks the syntactical uniformity of the expression. I think it is much clearer when each field is given a name(and a value) the same way.
berznizalmost 10 years ago
Styleguides are a matter of taste and there are probably no two people in the world with the same taste. Was there a voting of this?
wnevetsalmost 10 years ago
2.2 seems completely arbitrary to me. If your functions are so big that you need block scope, you&#x27;re doing it wrong IMO.
评论 #9823703 未加载
thomasfoster96almost 10 years ago
Having seen this updated version I&#x27;m not terribly impressed at their guide for ES6. No mention of symbols (which are pretty important if you&#x27;re using classes), generators are ignored almost entirely, a pretty poor explanation of modules, etc.
评论 #9823230 未加载
评论 #9823931 未加载
评论 #9823287 未加载
wes-expalmost 10 years ago
Airbnb&#x27;s technical quality has been obviously crap for its entire existence. Why are we taking engineering cues from a glorified room rental site that is frequently buggy?
评论 #9823749 未加载
评论 #9824367 未加载
joeblow99almost 10 years ago
&#x2F;&#x2F; bad const items = new Array();<p>&#x2F;&#x2F; good const items = [];<p>They obviously spent a lot of time on this guide, lots of investor dollars, and it&#x27;s of almost no use.
评论 #9823925 未加载
评论 #9824610 未加载
vivianLTPalmost 10 years ago
I&#x27;m a big fan of javascript.
joeblow99almost 10 years ago
&#x2F;&#x2F; bad const item = new Object();<p>&#x2F;&#x2F; good const item = {};<p>Literally useless differentiation.
评论 #9823719 未加载
评论 #9823945 未加载
评论 #9823908 未加载
评论 #9824040 未加载
jasonkesteralmost 10 years ago
I think I&#x27;d go nuts in a codebase with their whitespace and brackets rules.<p>Sure, cramming the opening bracket onto the previous line is just ugly and something you could learn to live with. But there&#x27;s a special type of rage that can only be generated by clicking on to the start of a line and having your cursor land 1-2 spaces to the left of it.<p>Why would anybody do that to their code voluntarily?
评论 #9823965 未加载
评论 #9823939 未加载
评论 #9823864 未加载
joeblow99almost 10 years ago
Use const for all of your references; avoid using var. If you must mutate references, use let instead of var.<p>In my 15 years of programming javascript I&#x27;ve never once seen this matter.
评论 #9823659 未加载