string
number
boolean
null
undefined
1 2 3 4 5 6 |
<span class="kwd">var</span><span class="pln"> foo </span><span class="pun">=</span> <span class="lit">1</span><span class="pun">,</span><span class="pln"> bar </span><span class="pun">=</span><span class="pln"> foo</span><span class="pun">;</span><span class="pln"> bar </span><span class="pun">=</span> <span class="lit">9</span><span class="pun">;</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">foo</span><span class="pun">,</span><span class="pln"> bar</span><span class="pun">);</span> <span class="com">// => 1, 9</span> |
复杂类型: 相当于传引用
object
array
function
1 2 3 4 5 6 |
<span class="kwd">var</span><span class="pln"> foo </span><span class="pun">=</span> <span class="pun">[</span><span class="lit">1</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">],</span><span class="pln"> bar </span><span class="pun">=</span><span class="pln"> foo</span><span class="pun">;</span><span class="pln"> bar</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span> <span class="pun">=</span> <span class="lit">9</span><span class="pun">;</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">foo</span><span class="pun">[</span><span class="lit">0</span><span class="pun">],</span><span class="pln"> bar</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]);</span> <span class="com">// => 9, 9</span> |
1 2 3 4 5 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> item </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Object</span><span class="pun">();</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> item </span><span class="pun">=</span> <span class="pun">{};</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> superman </span><span class="pun">=</span> <span class="pun">{</span> <span class="kwd">class</span><span class="pun">:</span> <span class="str">'superhero'</span><span class="pun">,</span> <span class="kwd">default</span><span class="pun">:</span> <span class="pun">{</span><span class="pln"> clark</span><span class="pun">:</span> <span class="str">'kent'</span> <span class="pun">},</span> <span class="kwd">private</span><span class="pun">:</span> <span class="kwd">true</span> <span class="pun">};</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> superman </span><span class="pun">=</span> <span class="pun">{</span><span class="pln"> klass</span><span class="pun">:</span> <span class="str">'superhero'</span><span class="pun">,</span><span class="pln"> defaults</span><span class="pun">:</span> <span class="pun">{</span><span class="pln"> clark</span><span class="pun">:</span> <span class="str">'kent'</span> <span class="pun">},</span><span class="pln"> hidden</span><span class="pun">:</span> <span class="kwd">true</span> <span class="pun">};</span> |
1 2 3 4 5 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> items </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Array</span><span class="pun">();</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> items </span><span class="pun">=</span> <span class="pun">[];</span> |
1 2 3 4 5 6 7 |
<span class="kwd">var</span><span class="pln"> someStack </span><span class="pun">=</span> <span class="pun">[];</span> <span class="com">// bad</span><span class="pln"> someStack</span><span class="pun">[</span><span class="pln">someStack</span><span class="pun">.</span><span class="pln">length</span><span class="pun">]</span> <span class="pun">=</span> <span class="str">'abracadabra'</span><span class="pun">;</span> <span class="com">// good</span><span class="pln"> someStack</span><span class="pun">.</span><span class="pln">push</span><span class="pun">(</span><span class="str">'abracadabra'</span><span class="pun">);</span> |
1 2 3 4 5 6 7 8 9 10 11 |
<span class="kwd">var</span><span class="pln"> len </span><span class="pun">=</span><span class="pln"> items</span><span class="pun">.</span><span class="pln">length</span><span class="pun">,</span><span class="pln"> itemsCopy </span><span class="pun">=</span> <span class="pun">[],</span><span class="pln"> i</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">for</span> <span class="pun">(</span><span class="pln">i </span><span class="pun">=</span> <span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="pln"> len</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span> <span class="pun">{</span><span class="pln"> itemsCopy</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> items</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span> <span class="pun">}</span> <span class="com">// good</span><span class="pln"> itemsCopy </span><span class="pun">=</span><span class="pln"> items</span><span class="pun">.</span><span class="pln">slice</span><span class="pun">();</span> |
1 2 3 4 |
<span class="kwd">function</span><span class="pln"> trigger</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">var</span><span class="pln"> args </span><span class="pun">=</span> <span class="pun">[].</span><span class="pln">slice</span><span class="pun">.</span><span class="pln">apply</span><span class="pun">(</span><span class="pln">arguments</span><span class="pun">);</span> <span class="pun">...</span> <span class="pun">}</span> |
''
(因为大多时候我们的字符串。特别html会出现"
)。
1 2 3 4 5 6 7 8 9 10 11 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span> <span class="str">"Bob Parr"</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span> <span class="str">'Bob Parr'</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> fullName </span><span class="pun">=</span> <span class="str">"Bob "</span> <span class="pun">+</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">lastName</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> fullName </span><span class="pun">=</span> <span class="str">'Bob '</span> <span class="pun">+</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">lastName</span><span class="pun">;</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> errorMessage </span><span class="pun">=</span> <span class="str">'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.'</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> errorMessage </span><span class="pun">=</span> <span class="str">'This is a super long error that \ was thrown because of Batman. \ When you stop to think about \ how Batman had anything to do \ with this, you would get nowhere \ fast.'</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> errorMessage </span><span class="pun">=</span> <span class="str">'This is a super long error that '</span> <span class="pun">+</span> <span class="str">'was thrown because of Batman.'</span> <span class="pun">+</span> <span class="str">'When you stop to think about '</span> <span class="pun">+</span> <span class="str">'how Batman had anything to do '</span> <span class="pun">+</span> <span class="str">'with this, you would get nowhere '</span> <span class="pun">+</span> <span class="str">'fast.'</span><span class="pun">;</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<span class="kwd">var</span><span class="pln"> items</span><span class="pun">,</span><span class="pln"> messages</span><span class="pun">,</span><span class="pln"> length</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">;</span><span class="pln"> messages </span><span class="pun">=</span> <span class="pun">[{</span><span class="pln"> state</span><span class="pun">:</span> <span class="str">'success'</span><span class="pun">,</span><span class="pln"> message</span><span class="pun">:</span> <span class="str">'This one worked.'</span> <span class="pun">},{</span><span class="pln"> state</span><span class="pun">:</span> <span class="str">'success'</span><span class="pun">,</span><span class="pln"> message</span><span class="pun">:</span> <span class="str">'This one worked as well.'</span> <span class="pun">},{</span><span class="pln"> state</span><span class="pun">:</span> <span class="str">'error'</span><span class="pun">,</span><span class="pln"> message</span><span class="pun">:</span> <span class="str">'This one did not work.'</span> <span class="pun">}];</span><span class="pln"> length </span><span class="pun">=</span><span class="pln"> messages</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">function</span><span class="pln"> inbox</span><span class="pun">(</span><span class="pln">messages</span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> items </span><span class="pun">=</span> <span class="str">'<ul>'</span><span class="pun">;</span> <span class="kwd">for</span> <span class="pun">(</span><span class="pln">i </span><span class="pun">=</span> <span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="pln"> length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span> <span class="pun">{</span><span class="pln"> items </span><span class="pun">+=</span> <span class="str">'<li>'</span> <span class="pun">+</span><span class="pln"> messages</span><span class="pun">[</span><span class="pln">i</span><span class="pun">].</span><span class="pln">message </span><span class="pun">+</span> <span class="str">'</li>'</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span><span class="pln"> items </span><span class="pun">+</span> <span class="str">'</ul>'</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pln"> inbox</span><span class="pun">(</span><span class="pln">messages</span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> items </span><span class="pun">=</span> <span class="pun">[];</span> <span class="kwd">for</span> <span class="pun">(</span><span class="pln">i </span><span class="pun">=</span> <span class="lit">0</span><span class="pun">;</span><span class="pln"> i </span><span class="pun"><</span><span class="pln"> length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span> <span class="pun">{</span><span class="pln"> items</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> messages</span><span class="pun">[</span><span class="pln">i</span><span class="pun">].</span><span class="pln">message</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span> <span class="str">'<ul><li>'</span> <span class="pun">+</span><span class="pln"> items</span><span class="pun">.</span><span class="pln">join</span><span class="pun">(</span><span class="str">'</li><li>'</span><span class="pun">)</span> <span class="pun">+</span> <span class="str">'</li></ul>'</span><span class="pun">;</span> <span class="pun">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="com">// 匿名函数表达式</span> <span class="kwd">var</span><span class="pln"> anonymous </span><span class="pun">=</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">true</span><span class="pun">;</span> <span class="pun">};</span> <span class="com">// 有名函数表达式</span> <span class="kwd">var</span><span class="pln"> named </span><span class="pun">=</span> <span class="kwd">function</span><span class="pln"> named</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">true</span><span class="pun">;</span> <span class="pun">};</span> <span class="com">// 立即调用函数表达式</span> <span class="pun">(</span><span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'Welcome to the Internet. Please follow me.'</span><span class="pun">);</span> <span class="pun">})();</span> |
块
定义为一组语句,函数声明不是一个语句。阅读ECMA-262对这个问题的说明。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="com">// bad</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">currentUser</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">function</span><span class="pln"> test</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'Nope.'</span><span class="pun">);</span> <span class="pun">}</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">currentUser</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">var</span><span class="pln"> test </span><span class="pun">=</span> <span class="kwd">function</span><span class="pln"> test</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'Yup.'</span><span class="pun">);</span> <span class="pun">};</span> <span class="pun">}</span> |
arguments
, 这将会逾越函数作用域内传过来的 arguments
对象。
1 2 3 4 5 6 7 8 9 |
<span class="com">// bad</span> <span class="kwd">function</span><span class="pln"> nope</span><span class="pun">(</span><span class="pln">name</span><span class="pun">,</span><span class="pln"> options</span><span class="pun">,</span><span class="pln"> arguments</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pln"> yup</span><span class="pun">(</span><span class="pln">name</span><span class="pun">,</span><span class="pln"> options</span><span class="pun">,</span><span class="pln"> args</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="pun">}</span> |
.
优先)。
1 2 3 4 5 6 7 8 9 10 |
<span class="kwd">var</span><span class="pln"> luke </span><span class="pun">=</span> <span class="pun">{</span><span class="pln"> jedi</span><span class="pun">:</span> <span class="kwd">true</span><span class="pun">,</span><span class="pln"> age</span><span class="pun">:</span> <span class="lit">28</span> <span class="pun">};</span> <span class="kwd">function</span><span class="pln"> getProp</span><span class="pun">(</span><span class="pln">prop</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span><span class="pln"> luke</span><span class="pun">[</span><span class="pln">prop</span><span class="pun">];</span> <span class="pun">}</span> <span class="kwd">var</span><span class="pln"> isJedi </span><span class="pun">=</span><span class="pln"> getProp</span><span class="pun">(</span><span class="str">'jedi'</span><span class="pun">);</span> |
var
来声明变量,如果不这么做将导致产生全局变量,我们要避免污染全局命名空间。
1 2 3 4 5 |
<span class="com">// bad</span><span class="pln"> superPower </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">SuperPower</span><span class="pun">();</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> superPower </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">SuperPower</span><span class="pun">();</span> |
var
以及新行声明多个变量,缩进4个空格。
1 2 3 4 5 6 7 8 9 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> items </span><span class="pun">=</span><span class="pln"> getItems</span><span class="pun">();</span> <span class="kwd">var</span><span class="pln"> goSportsTeam </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">;</span> <span class="kwd">var</span><span class="pln"> dragonball </span><span class="pun">=</span> <span class="str">'z'</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> items </span><span class="pun">=</span><span class="pln"> getItems</span><span class="pun">(),</span><span class="pln"> goSportsTeam </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">,</span><span class="pln"> dragonball </span><span class="pun">=</span> <span class="str">'z'</span><span class="pun">;</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> len</span><span class="pun">,</span><span class="pln"> dragonball</span><span class="pun">,</span><span class="pln"> items </span><span class="pun">=</span><span class="pln"> getItems</span><span class="pun">(),</span><span class="pln"> goSportsTeam </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> items </span><span class="pun">=</span><span class="pln"> getItems</span><span class="pun">(),</span><span class="pln"> dragonball</span><span class="pun">,</span><span class="pln"> goSportsTeam </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">,</span><span class="pln"> len</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> items </span><span class="pun">=</span><span class="pln"> getItems</span><span class="pun">(),</span><span class="pln"> goSportsTeam </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">,</span><span class="pln"> dragonball</span><span class="pun">,</span><span class="pln"> length</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">;</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<span class="com">// bad</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> test</span><span class="pun">();</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'doing stuff..'</span><span class="pun">);</span> <span class="com">//..other stuff..</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span><span class="pln"> getName</span><span class="pun">();</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">name </span><span class="pun">===</span> <span class="str">'test'</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span><span class="pln"> name</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span><span class="pln"> getName</span><span class="pun">();</span><span class="pln"> test</span><span class="pun">();</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'doing stuff..'</span><span class="pun">);</span> <span class="com">//..other stuff..</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">name </span><span class="pun">===</span> <span class="str">'test'</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span><span class="pln"> name</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// bad</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">if</span> <span class="pun">(!</span><span class="pln">arguments</span><span class="pun">.</span><span class="pln">length</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span><span class="pln"> getName</span><span class="pun">();</span> <span class="kwd">return</span> <span class="kwd">true</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span><span class="pln"> getName</span><span class="pun">();</span> <span class="kwd">if</span> <span class="pun">(!</span><span class="pln">arguments</span><span class="pun">.</span><span class="pln">length</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span> <span class="kwd">true</span><span class="pun">;</span> <span class="pun">}</span> |
===
和 !==
以及 ==
和 !=
。''
则被计算为 false, 否则为 true
1 2 3 4 |
<span class="kwd">if</span> <span class="pun">([</span><span class="lit">0</span><span class="pun">])</span> <span class="pun">{</span> <span class="com">// true</span> <span class="com">// An array is an object, objects evaluate to true</span> <span class="pun">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span class="com">// bad</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">name </span><span class="pun">!==</span> <span class="str">''</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">name</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="pun">}</span> <span class="com">// bad</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">collection</span><span class="pun">.</span><span class="pln">length </span><span class="pun">></span> <span class="lit">0</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">collection</span><span class="pun">.</span><span class="pln">length</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="pun">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span class="com">// bad</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">test</span><span class="pun">)</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">test</span><span class="pun">)</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">test</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// bad</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> |
/** ... */
进行多行注释,包括描述,指定类型以及参数值和返回值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<span class="com">// bad</span> <span class="com">// make() returns a new element</span> <span class="com">// based on the passed in tag name</span> <span class="com">//</span> <span class="com">// @param <String> tag</span> <span class="com">// @return <Element> element</span> <span class="kwd">function</span><span class="pln"> make</span><span class="pun">(</span><span class="pln">tag</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="kwd">return</span><span class="pln"> element</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// good</span> <span class="com">/** * make() returns a new element * based on the passed in tag name * * @param <String> tag * @return <Element> element */</span> <span class="kwd">function</span><span class="pln"> make</span><span class="pun">(</span><span class="pln">tag</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="kwd">return</span><span class="pln"> element</span><span class="pun">;</span> <span class="pun">}</span> |
//
进行单行注释,在评论对象的上面进行单行注释,注释前放一个空行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> active </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">;</span> <span class="com">// is current tab</span> <span class="com">// good</span> <span class="com">// is current tab</span> <span class="kwd">var</span><span class="pln"> active </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">function</span><span class="pln"> getType</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'fetching type...'</span><span class="pun">);</span> <span class="com">// set the default type to 'no type'</span> <span class="kwd">var</span><span class="pln"> type </span><span class="pun">=</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">_type </span><span class="pun">||</span> <span class="str">'no type'</span><span class="pun">;</span> <span class="kwd">return</span><span class="pln"> type</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pln"> getType</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'fetching type...'</span><span class="pun">);</span> <span class="com">// set the default type to 'no type'</span> <span class="kwd">var</span><span class="pln"> type </span><span class="pun">=</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">_type </span><span class="pun">||</span> <span class="str">'no type'</span><span class="pun">;</span> <span class="kwd">return</span><span class="pln"> type</span><span class="pun">;</span> <span class="pun">}</span> |
FIXME
或 TODO
帮助其他人迅速理解。
1 2 3 4 5 6 7 |
<span class="kwd">function</span> <span class="typ">Calculator</span><span class="pun">()</span> <span class="pun">{</span> <span class="com">// FIXME: shouldn't use a global here</span><span class="pln"> total </span><span class="pun">=</span> <span class="lit">0</span><span class="pun">;</span> <span class="kwd">return</span> <span class="kwd">this</span><span class="pun">;</span> <span class="pun">}</span> |
1 2 3 4 5 6 7 |
<span class="kwd">function</span> <span class="typ">Calculator</span><span class="pun">()</span> <span class="pun">{</span> <span class="com">// TODO: total should be configurable by an options param</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">total </span><span class="pun">=</span> <span class="lit">0</span><span class="pun">;</span> <span class="kwd">return</span> <span class="kwd">this</span><span class="pun">;</span> <span class="pun">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="com">// bad</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="pun">∙∙</span><span class="kwd">var</span><span class="pln"> name</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// bad</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="pun">∙</span><span class="kwd">var</span><span class="pln"> name</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="pun">∙∙∙∙</span><span class="kwd">var</span><span class="pln"> name</span><span class="pun">;</span> <span class="pun">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="com">// bad</span> <span class="kwd">function</span><span class="pln"> test</span><span class="pun">(){</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'test'</span><span class="pun">);</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pln"> test</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'test'</span><span class="pun">);</span> <span class="pun">}</span> <span class="com">// bad</span><span class="pln"> dog</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="str">'attr'</span><span class="pun">,{</span><span class="pln"> age</span><span class="pun">:</span> <span class="str">'1 year'</span><span class="pun">,</span><span class="pln"> breed</span><span class="pun">:</span> <span class="str">'Bernese Mountain Dog'</span> <span class="pun">});</span> <span class="com">// good</span><span class="pln"> dog</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="str">'attr'</span><span class="pun">,</span> <span class="pun">{</span><span class="pln"> age</span><span class="pun">:</span> <span class="str">'1 year'</span><span class="pun">,</span><span class="pln"> breed</span><span class="pun">:</span> <span class="str">'Bernese Mountain Dog'</span> <span class="pun">});</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<span class="com">// bad</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#items'</span><span class="pun">).</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.selected'</span><span class="pun">).</span><span class="pln">highlight</span><span class="pun">().</span><span class="kwd">end</span><span class="pun">().</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.open'</span><span class="pun">).</span><span class="pln">updateCount</span><span class="pun">();</span> <span class="com">// good</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#items'</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.selected'</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">highlight</span><span class="pun">()</span> <span class="pun">.</span><span class="kwd">end</span><span class="pun">()</span> <span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'.open'</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">updateCount</span><span class="pun">();</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> leds </span><span class="pun">=</span><span class="pln"> stage</span><span class="pun">.</span><span class="pln">selectAll</span><span class="pun">(</span><span class="str">'.led'</span><span class="pun">).</span><span class="pln">data</span><span class="pun">(</span><span class="pln">data</span><span class="pun">).</span><span class="pln">enter</span><span class="pun">().</span><span class="pln">append</span><span class="pun">(</span><span class="str">'svg:svg'</span><span class="pun">).</span><span class="kwd">class</span><span class="pun">(</span><span class="str">'led'</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">attr</span><span class="pun">(</span><span class="str">'width'</span><span class="pun">,</span> <span class="pun">(</span><span class="pln">radius </span><span class="pun">+</span><span class="pln"> margin</span><span class="pun">)</span> <span class="pun">*</span> <span class="lit">2</span><span class="pun">).</span><span class="pln">append</span><span class="pun">(</span><span class="str">'svg:g'</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">attr</span><span class="pun">(</span><span class="str">'transform'</span><span class="pun">,</span> <span class="str">'translate('</span> <span class="pun">+</span> <span class="pun">(</span><span class="pln">radius </span><span class="pun">+</span><span class="pln"> margin</span><span class="pun">)</span> <span class="pun">+</span> <span class="str">','</span> <span class="pun">+</span> <span class="pun">(</span><span class="pln">radius </span><span class="pun">+</span><span class="pln"> margin</span><span class="pun">)</span> <span class="pun">+</span> <span class="str">')'</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">call</span><span class="pun">(</span><span class="pln">tron</span><span class="pun">.</span><span class="pln">led</span><span class="pun">);</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> leds </span><span class="pun">=</span><span class="pln"> stage</span><span class="pun">.</span><span class="pln">selectAll</span><span class="pun">(</span><span class="str">'.led'</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">data</span><span class="pun">(</span><span class="pln">data</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">enter</span><span class="pun">().</span><span class="pln">append</span><span class="pun">(</span><span class="str">'svg:svg'</span><span class="pun">)</span> <span class="pun">.</span><span class="kwd">class</span><span class="pun">(</span><span class="str">'led'</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">attr</span><span class="pun">(</span><span class="str">'width'</span><span class="pun">,</span> <span class="pun">(</span><span class="pln">radius </span><span class="pun">+</span><span class="pln"> margin</span><span class="pun">)</span> <span class="pun">*</span> <span class="lit">2</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">append</span><span class="pun">(</span><span class="str">'svg:g'</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">attr</span><span class="pun">(</span><span class="str">'transform'</span><span class="pun">,</span> <span class="str">'translate('</span> <span class="pun">+</span> <span class="pun">(</span><span class="pln">radius </span><span class="pun">+</span><span class="pln"> margin</span><span class="pun">)</span> <span class="pun">+</span> <span class="str">','</span> <span class="pun">+</span> <span class="pun">(</span><span class="pln">radius </span><span class="pun">+</span><span class="pln"> margin</span><span class="pun">)</span> <span class="pun">+</span> <span class="str">')'</span><span class="pun">)</span> <span class="pun">.</span><span class="pln">call</span><span class="pun">(</span><span class="pln">tron</span><span class="pun">.</span><span class="pln">led</span><span class="pun">);</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> once </span><span class="pun">,</span><span class="pln"> upon </span><span class="pun">,</span><span class="pln"> aTime</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> once</span><span class="pun">,</span><span class="pln"> upon</span><span class="pun">,</span><span class="pln"> aTime</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> hero </span><span class="pun">=</span> <span class="pun">{</span><span class="pln"> firstName</span><span class="pun">:</span> <span class="str">'Bob'</span> <span class="pun">,</span><span class="pln"> lastName</span><span class="pun">:</span> <span class="str">'Parr'</span> <span class="pun">,</span><span class="pln"> heroName</span><span class="pun">:</span> <span class="str">'Mr. Incredible'</span> <span class="pun">,</span><span class="pln"> superPower</span><span class="pun">:</span> <span class="str">'strength'</span> <span class="pun">};</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> hero </span><span class="pun">=</span> <span class="pun">{</span><span class="pln"> firstName</span><span class="pun">:</span> <span class="str">'Bob'</span><span class="pun">,</span><span class="pln"> lastName</span><span class="pun">:</span> <span class="str">'Parr'</span><span class="pun">,</span><span class="pln"> heroName</span><span class="pun">:</span> <span class="str">'Mr. Incredible'</span><span class="pun">,</span><span class="pln"> superPower</span><span class="pun">:</span> <span class="str">'strength'</span> <span class="pun">};</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> hero </span><span class="pun">=</span> <span class="pun">{</span><span class="pln"> firstName</span><span class="pun">:</span> <span class="str">'Kevin'</span><span class="pun">,</span><span class="pln"> lastName</span><span class="pun">:</span> <span class="str">'Flynn'</span><span class="pun">,</span> <span class="pun">};</span> <span class="kwd">var</span><span class="pln"> heroes </span><span class="pun">=</span> <span class="pun">[</span> <span class="str">'Batman'</span><span class="pun">,</span> <span class="str">'Superman'</span><span class="pun">,</span> <span class="pun">];</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> hero </span><span class="pun">=</span> <span class="pun">{</span><span class="pln"> firstName</span><span class="pun">:</span> <span class="str">'Kevin'</span><span class="pun">,</span><span class="pln"> lastName</span><span class="pun">:</span> <span class="str">'Flynn'</span> <span class="pun">};</span> <span class="kwd">var</span><span class="pln"> heroes </span><span class="pun">=</span> <span class="pun">[</span> <span class="str">'Batman'</span><span class="pun">,</span> <span class="str">'Superman'</span> <span class="pun">];</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="com">// bad</span> <span class="pun">(</span><span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span> <span class="str">'Skywalker'</span> <span class="kwd">return</span><span class="pln"> name </span><span class="pun">})()</span> <span class="com">// good</span> <span class="pun">(</span><span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span> <span class="str">'Skywalker'</span><span class="pun">;</span> <span class="kwd">return</span><span class="pln"> name</span><span class="pun">;</span> <span class="pun">})();</span> <span class="com">// good</span> <span class="pun">;(</span><span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">var</span><span class="pln"> name </span><span class="pun">=</span> <span class="str">'Skywalker'</span><span class="pun">;</span> <span class="kwd">return</span><span class="pln"> name</span><span class="pun">;</span> <span class="pun">})();</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="com">// => this.reviewScore = 9;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> totalScore </span><span class="pun">=</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">reviewScore </span><span class="pun">+</span> <span class="str">''</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> totalScore </span><span class="pun">=</span> <span class="str">''</span> <span class="pun">+</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">reviewScore</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> totalScore </span><span class="pun">=</span> <span class="str">''</span> <span class="pun">+</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">reviewScore </span><span class="pun">+</span> <span class="str">' total score'</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> totalScore </span><span class="pun">=</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">reviewScore </span><span class="pun">+</span> <span class="str">' total score'</span><span class="pun">;</span> |
parseInt
并且总是带上类型转换的基数,如parseInt(value, 10)
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<span class="kwd">var</span><span class="pln"> inputValue </span><span class="pun">=</span> <span class="str">'4'</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> val </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Number</span><span class="pun">(</span><span class="pln">inputValue</span><span class="pun">);</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> val </span><span class="pun">=</span> <span class="pun">+</span><span class="pln">inputValue</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> val </span><span class="pun">=</span><span class="pln"> inputValue </span><span class="pun">>></span> <span class="lit">0</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> val </span><span class="pun">=</span><span class="pln"> parseInt</span><span class="pun">(</span><span class="pln">inputValue</span><span class="pun">);</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> val </span><span class="pun">=</span> <span class="typ">Number</span><span class="pun">(</span><span class="pln">inputValue</span><span class="pun">);</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> val </span><span class="pun">=</span><span class="pln"> parseInt</span><span class="pun">(</span><span class="pln">inputValue</span><span class="pun">,</span> <span class="lit">10</span><span class="pun">);</span> <span class="com">// good</span> <span class="com">/** * parseInt was the reason my code was slow. * Bitshifting the String to coerce it to a * Number made it a lot faster. */</span> <span class="kwd">var</span><span class="pln"> val </span><span class="pun">=</span><span class="pln"> inputValue </span><span class="pun">>></span> <span class="lit">0</span><span class="pun">;</span> |
1 2 3 4 5 6 7 8 9 10 |
<span class="kwd">var</span><span class="pln"> age </span><span class="pun">=</span> <span class="lit">0</span><span class="pun">;</span> <span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> hasAge </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Boolean</span><span class="pun">(</span><span class="pln">age</span><span class="pun">);</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> hasAge </span><span class="pun">=</span> <span class="typ">Boolean</span><span class="pun">(</span><span class="pln">age</span><span class="pun">);</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> hasAge </span><span class="pun">=</span> <span class="pun">!!</span><span class="pln">age</span><span class="pun">;</span> |
1 2 3 4 5 6 7 8 9 |
<span class="com">// bad</span> <span class="kwd">function</span><span class="pln"> q</span><span class="pun">()</span> <span class="pun">{</span> <span class="com">// ...stuff...</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pln"> query</span><span class="pun">()</span> <span class="pun">{</span> <span class="com">// ..stuff..</span> <span class="pun">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span class="com">// bad</span> <span class="kwd">var</span> <span class="typ">OBJEcttsssss</span> <span class="pun">=</span> <span class="pun">{};</span> <span class="kwd">var</span><span class="pln"> this_is_my_object </span><span class="pun">=</span> <span class="pun">{};</span> <span class="kwd">var</span> <span class="kwd">this</span><span class="pun">-</span><span class="kwd">is</span><span class="pun">-</span><span class="kwd">my</span><span class="pun">-</span><span class="kwd">object</span> <span class="pun">=</span> <span class="pun">{};</span> <span class="kwd">function</span><span class="pln"> c</span><span class="pun">()</span> <span class="pun">{};</span> <span class="kwd">var</span><span class="pln"> u </span><span class="pun">=</span> <span class="kwd">new</span><span class="pln"> user</span><span class="pun">({</span><span class="pln"> name</span><span class="pun">:</span> <span class="str">'Bob Parr'</span> <span class="pun">});</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> thisIsMyObject </span><span class="pun">=</span> <span class="pun">{};</span> <span class="kwd">function</span><span class="pln"> thisIsMyFunction</span><span class="pun">()</span> <span class="pun">{};</span> <span class="kwd">var</span><span class="pln"> user </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">User</span><span class="pun">({</span><span class="pln"> name</span><span class="pun">:</span> <span class="str">'Bob Parr'</span> <span class="pun">});</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="com">// bad</span> <span class="kwd">function</span><span class="pln"> user</span><span class="pun">(</span><span class="pln">options</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">name </span><span class="pun">=</span><span class="pln"> options</span><span class="pun">.</span><span class="pln">name</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">var</span><span class="pln"> bad </span><span class="pun">=</span> <span class="kwd">new</span><span class="pln"> user</span><span class="pun">({</span><span class="pln"> name</span><span class="pun">:</span> <span class="str">'nope'</span> <span class="pun">});</span> <span class="com">// good</span> <span class="kwd">function</span> <span class="typ">User</span><span class="pun">(</span><span class="pln">options</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">name </span><span class="pun">=</span><span class="pln"> options</span><span class="pun">.</span><span class="pln">name</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">var</span><span class="pln"> good </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">User</span><span class="pun">({</span><span class="pln"> name</span><span class="pun">:</span> <span class="str">'yup'</span> <span class="pun">});</span> |
_
:
1 2 3 4 5 6 |
<span class="com">// bad</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">__firstName__ </span><span class="pun">=</span> <span class="str">'Panda'</span><span class="pun">;</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">firstName_ </span><span class="pun">=</span> <span class="str">'Panda'</span><span class="pun">;</span> <span class="com">// good</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">_firstName </span><span class="pun">=</span> <span class="str">'Panda'</span><span class="pun">;</span> |
this
的引用时使用 self(python 风格)
,避免this issue
.Angular建议使用vm(MVVM模式中view-model)
:
1 2 3 4 5 6 7 |
<span class="com">// good</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">var</span> <span class="kwd">self</span> <span class="pun">=</span> <span class="kwd">this</span><span class="pun">;</span> <span class="kwd">return</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">);</span> <span class="pun">};</span> <span class="pun">}</span> |
java getter、setter风格
或者jQuery风格
。
1 2 3 4 5 6 7 8 9 |
<span class="com">// bad</span> <span class="kwd">if</span> <span class="pun">(!</span><span class="pln">dragon</span><span class="pun">.</span><span class="pln">age</span><span class="pun">())</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">if</span> <span class="pun">(!</span><span class="pln">dragon</span><span class="pun">.</span><span class="pln">hasAge</span><span class="pun">())</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">false</span><span class="pun">;</span> <span class="pun">}</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="kwd">function</span> <span class="typ">Jedi</span><span class="pun">(</span><span class="pln">options</span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> options </span><span class="pun">||</span> <span class="pun">(</span><span class="pln">options </span><span class="pun">=</span> <span class="pun">{});</span> <span class="kwd">var</span><span class="pln"> lightsaber </span><span class="pun">=</span><span class="pln"> options</span><span class="pun">.</span><span class="pln">lightsaber </span><span class="pun">||</span> <span class="str">'blue'</span><span class="pun">;</span> <span class="kwd">this</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="str">'lightsaber'</span><span class="pun">,</span><span class="pln"> lightsaber</span><span class="pun">);</span> <span class="pun">}</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="kwd">set</span> <span class="pun">=</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">key</span><span class="pun">,</span><span class="pln"> val</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">this</span><span class="pun">[</span><span class="pln">key</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> val</span><span class="pun">;</span> <span class="pun">};</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="kwd">get</span> <span class="pun">=</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">key</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">this</span><span class="pun">[</span><span class="pln">key</span><span class="pun">];</span> <span class="pun">};</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span class="kwd">function</span> <span class="typ">Jedi</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'new jedi'</span><span class="pun">);</span> <span class="pun">}</span> <span class="com">// bad</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype </span><span class="pun">=</span> <span class="pun">{</span><span class="pln"> fight</span><span class="pun">:</span> <span class="kwd">function</span><span class="pln"> fight</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'fighting'</span><span class="pun">);</span> <span class="pun">},</span><span class="pln"> block</span><span class="pun">:</span> <span class="kwd">function</span><span class="pln"> block</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'blocking'</span><span class="pun">);</span> <span class="pun">}</span> <span class="pun">};</span> <span class="com">// good</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">fight </span><span class="pun">=</span> <span class="kwd">function</span><span class="pln"> fight</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'fighting'</span><span class="pun">);</span> <span class="pun">};</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">block </span><span class="pun">=</span> <span class="kwd">function</span><span class="pln"> block</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'blocking'</span><span class="pun">);</span> <span class="pun">};</span> |
this
帮助方法可链。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<span class="com">// bad</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">jump </span><span class="pun">=</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">jumping </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">;</span> <span class="kwd">return</span> <span class="kwd">true</span><span class="pun">;</span> <span class="pun">};</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">setHeight </span><span class="pun">=</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">height</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">height </span><span class="pun">=</span><span class="pln"> height</span><span class="pun">;</span> <span class="pun">};</span> <span class="kwd">var</span><span class="pln"> luke </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Jedi</span><span class="pun">();</span><span class="pln"> luke</span><span class="pun">.</span><span class="pln">jump</span><span class="pun">();</span> <span class="com">// => true</span><span class="pln"> luke</span><span class="pun">.</span><span class="pln">setHeight</span><span class="pun">(</span><span class="lit">20</span><span class="pun">)</span> <span class="com">// => undefined</span> <span class="com">// good</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">jump </span><span class="pun">=</span> <span class="kwd">function</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">jumping </span><span class="pun">=</span> <span class="kwd">true</span><span class="pun">;</span> <span class="kwd">return</span> <span class="kwd">this</span><span class="pun">;</span> <span class="pun">};</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">setHeight </span><span class="pun">=</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">height</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">height </span><span class="pun">=</span><span class="pln"> height</span><span class="pun">;</span> <span class="kwd">return</span> <span class="kwd">this</span><span class="pun">;</span> <span class="pun">};</span> <span class="kwd">var</span><span class="pln"> luke </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Jedi</span><span class="pun">();</span><span class="pln"> luke</span><span class="pun">.</span><span class="pln">jump</span><span class="pun">()</span> <span class="pun">.</span><span class="pln">setHeight</span><span class="pun">(</span><span class="lit">20</span><span class="pun">);</span> |
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="kwd">function</span> <span class="typ">Jedi</span><span class="pun">(</span><span class="pln">options</span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> options </span><span class="pun">||</span> <span class="pun">(</span><span class="pln">options </span><span class="pun">=</span> <span class="pun">{});</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">name </span><span class="pun">=</span><span class="pln"> options</span><span class="pun">.</span><span class="pln">name </span><span class="pun">||</span> <span class="str">'no name'</span><span class="pun">;</span> <span class="pun">}</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">getName </span><span class="pun">=</span> <span class="kwd">function</span><span class="pln"> getName</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">name</span><span class="pun">;</span> <span class="pun">};</span> <span class="typ">Jedi</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">toString </span><span class="pun">=</span> <span class="kwd">function</span><span class="pln"> toString</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="str">'Jedi - '</span> <span class="pun">+</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">getName</span><span class="pun">();</span> <span class="pun">};</span> |
1 2 3 4 5 6 7 8 |
<span class="com">// bad</span><span class="pln"> $</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">).</span><span class="pln">trigger</span><span class="pun">(</span><span class="str">'listingUpdated'</span><span class="pun">,</span><span class="pln"> listing</span><span class="pun">.</span><span class="pln">id</span><span class="pun">);</span> <span class="pun">...</span><span class="pln"> $</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">).</span><span class="pln">on</span><span class="pun">(</span><span class="str">'listingUpdated'</span><span class="pun">,</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">,</span><span class="pln"> listingId</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// do something with listingId</span> <span class="pun">});</span> |
1 2 3 4 5 6 7 8 |
<span class="com">// good</span><span class="pln"> $</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">).</span><span class="pln">trigger</span><span class="pun">(</span><span class="str">'listingUpdated'</span><span class="pun">,</span> <span class="pun">{</span><span class="pln"> listingId </span><span class="pun">:</span><span class="pln"> listing</span><span class="pun">.</span><span class="pln">id </span><span class="pun">});</span> <span class="pun">...</span><span class="pln"> $</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">).</span><span class="pln">on</span><span class="pun">(</span><span class="str">'listingUpdated'</span><span class="pun">,</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">,</span><span class="pln"> data</span><span class="pun">)</span> <span class="pun">{</span> <span class="com">// do something with data.listingId</span> <span class="pun">});</span> |
'use strict';
,引入[JSHint规范](http://jshint.com/)
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span class="com">// fancyInput/fancyInput.js</span> <span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="kwd">global</span><span class="pun">)</span> <span class="pun">{</span> <span class="str">'use strict'</span><span class="pun">;</span> <span class="kwd">var</span><span class="pln"> previousFancyInput </span><span class="pun">=</span> <span class="kwd">global</span><span class="pun">.</span><span class="typ">FancyInput</span><span class="pun">;</span> <span class="kwd">function</span> <span class="typ">FancyInput</span><span class="pun">(</span><span class="pln">options</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">this</span><span class="pun">.</span><span class="pln">options </span><span class="pun">=</span><span class="pln"> options </span><span class="pun">||</span> <span class="pun">{};</span> <span class="pun">}</span> <span class="typ">FancyInput</span><span class="pun">.</span><span class="pln">noConflict </span><span class="pun">=</span> <span class="kwd">function</span><span class="pln"> noConflict</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">global</span><span class="pun">.</span><span class="typ">FancyInput</span> <span class="pun">=</span><span class="pln"> previousFancyInput</span><span class="pun">;</span> <span class="kwd">return</span> <span class="typ">FancyInput</span><span class="pun">;</span> <span class="pun">};</span> <span class="kwd">global</span><span class="pun">.</span><span class="typ">FancyInput</span> <span class="pun">=</span> <span class="typ">FancyInput</span><span class="pun">;</span> <span class="pun">})(</span><span class="kwd">this</span><span class="pun">);</span> |
$
开头,以和原生DOM节点区分。
1 2 3 4 5 |
<span class="com">// bad</span> <span class="kwd">var</span><span class="pln"> menu </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">".menu"</span><span class="pun">);</span> <span class="com">// good</span> <span class="kwd">var</span><span class="pln"> $menu </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">".menu"</span><span class="pun">);</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<span class="com">// bad</span> <span class="kwd">function</span><span class="pln"> setSidebar</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'.sidebar'</span><span class="pun">).</span><span class="pln">hide</span><span class="pun">();</span> <span class="com">// ...stuff...</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'.sidebar'</span><span class="pun">).</span><span class="pln">css</span><span class="pun">({</span> <span class="str">'background-color'</span><span class="pun">:</span> <span class="str">'pink'</span> <span class="pun">});</span> <span class="pun">}</span> <span class="com">// good</span> <span class="kwd">function</span><span class="pln"> setSidebar</span><span class="pun">()</span> <span class="pun">{</span> <span class="kwd">var</span><span class="pln"> $sidebar </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'.sidebar'</span><span class="pun">);</span><span class="pln"> $sidebar</span><span class="pun">.</span><span class="pln">hide</span><span class="pun">();</span> <span class="com">// ...stuff...</span><span class="pln"> $sidebar</span><span class="pun">.</span><span class="pln">css</span><span class="pun">({</span> <span class="str">'background-color'</span><span class="pun">:</span> <span class="str">'pink'</span> <span class="pun">});</span> <span class="pun">}</span> |
$('.sidebar ul')
或 $('.sidebar ul')
,jsPerf。find
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="com">// bad</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'.sidebar'</span><span class="pun">,</span> <span class="str">'ul'</span><span class="pun">).</span><span class="pln">hide</span><span class="pun">();</span> <span class="com">// bad</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'.sidebar'</span><span class="pun">).</span><span class="pln">find</span><span class="pun">(</span><span class="str">'ul'</span><span class="pun">).</span><span class="pln">hide</span><span class="pun">();</span> <span class="com">// good</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'.sidebar ul'</span><span class="pun">).</span><span class="pln">hide</span><span class="pun">();</span> <span class="com">// good</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'.sidebar > ul'</span><span class="pun">).</span><span class="pln">hide</span><span class="pun">();</span> <span class="com">// good (slower)</span><span class="pln"> $sidebar</span><span class="pun">.</span><span class="pln">find</span><span class="pun">(</span><span class="str">'ul'</span><span class="pun">);</span> <span class="com">// good (faster)</span><span class="pln"> $</span><span class="pun">(</span><span class="pln">$sidebar</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]).</span><span class="pln">find</span><span class="pun">(</span><span class="str">'ul'</span><span class="pun">);</span> |
1 2 3 |
<span class="pln">$</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(){</span> <span class="com">//do your page init. </span> <span class="pun">});</span> |
jQuery.on
从页面分离到JavaScript文件。
1 2 3 4 5 6 7 |
<span class="com">// bad</span> <span class="pun"><</span><span class="pln">a id</span><span class="pun">=</span><span class="str">"myLink"</span><span class="pln"> href</span><span class="pun">=</span><span class="str">"#"</span><span class="pln"> onclick</span><span class="pun">=</span><span class="str">"myEventHandler();"</span><span class="pun">></</span><span class="pln">a</span><span class="pun">></span> <span class="com">// good</span> <span class="pun"><</span><span class="pln">a id</span><span class="pun">=</span><span class="str">"myLink"</span><span class="pln"> href</span><span class="pun">=</span><span class="str">"#"</span><span class="pun">></</span><span class="pln">a</span><span class="pun">></span><span class="pln"> $</span><span class="pun">(</span><span class="str">"#myLink"</span><span class="pun">).</span><span class="pln">on</span><span class="pun">(</span><span class="str">"click"</span><span class="pun">,</span><span class="pln"> myEventHandler</span><span class="pun">);</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="com">// bad</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">ajax</span><span class="pun">({</span> <span class="pun">...</span><span class="pln"> success </span><span class="pun">:</span> <span class="kwd">function</span><span class="pun">(){</span> <span class="pun">},</span><span class="pln"> error </span><span class="pun">:</span> <span class="kwd">function</span><span class="pun">(){</span> <span class="pun">}</span> <span class="pun">})</span> <span class="com">// good</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">ajax</span><span class="pun">({.</span> <span class="pun">..</span> <span class="pun">}).</span><span class="kwd">then</span><span class="pun">(</span> <span class="kwd">function</span><span class="pun">(</span> <span class="pun">){</span> <span class="com">// success</span> <span class="pun">},</span> <span class="kwd">function</span><span class="pun">(</span> <span class="pun">){</span> <span class="com">// error</span> <span class="pun">})</span> |
1 2 3 4 5 6 7 8 9 |
<span class="kwd">var</span><span class="pln"> dtd </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">.</span><span class="typ">Deferred</span><span class="pun">();</span> <span class="com">// 新建一个deferred对象</span><span class="pln"> </span><span class="kwd">var</span><span class="pln"> wait </span><span class="pun">=</span> <span class="kwd">function</span><span class="pun">(</span><span class="pln">dtd</span><span class="pun">){</span><span class="pln"> </span><span class="kwd">var</span><span class="pln"> tasks </span><span class="pun">=</span> <span class="kwd">function</span><span class="pun">(){</span><span class="pln"> alert</span><span class="pun">(</span><span class="str">"执行完毕!"</span><span class="pun">);</span><span class="pln"> dtd</span><span class="pun">.</span><span class="pln">resolve</span><span class="pun">();</span> <span class="com">// 改变deferred对象的执行状态</span><span class="pln"> </span><span class="pun">};</span><span class="pln"> setTimeout</span><span class="pun">(</span><span class="pln">tasks</span><span class="pun">,</span><span class="lit">5000</span><span class="pun">);</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> dtd</span><span class="pun">;</span><span class="pln"> </span><span class="pun">};</span> |
尽量采用ES5方法,特别数组map、filter、forEach方法简化日常开发。在老式IE浏览器中引入ES5-shim。或者也可以考虑引入underscore、lodash 常用辅助库。
– 参考Kangax的 ES5 compatibility table。
bower install jQuery
),bower可以实现第三方库的依赖解析、下载、升级管理等。建议建立企业级bower私服。来源:http://greengerong.github.io/blog/2015/05/09/qian-duan-javascriptgui-fan
from:https://www.runoob.com/w3cnote/javascript-guide.html