Javascript常用小技巧匯總

來源:互聯網
上載者:User

   本文執行個體講述了Javascript常用小技巧。分享給大家供大家參考。具體分析如下:

  一、True 和 False 布林運算式

  下面的布林運算式都返回 false:

  null

  undefined

  '' Null 字元串

  0 數字0

  但小心下面的, 可都返回 true:

  '0' 字串0

  [] 空數組

  {} Null 物件

  下面段比較糟糕的代碼:

   代碼如下:

  while (x != null) {

  你可以直接寫成下面的形式(只要你希望 x 不是 0 和Null 字元串, 和 false):

   代碼如下:

  while (x) {

  如果你想檢查字串是否為 null 或空:

   代碼如下:

  if (y != null && y != '') {

  但這樣會更好:

   代碼如下:

  if (y) {

  注意: 還有很多需要注意的地方, 如:

  Boolean('0') == true

  '0' != true

  0 != null

  0 == []

  0 == false

  Boolean(null) ==false

  null != true

  null != false

  Boolean(undefined) ==false

  undefined != true

  undefined != false

  Boolean([]) == true

  [] != true

  [] == false

  Boolean({}) == true

  {} != true

  {} != false

  二、條件(三元)操作符 (?:)

  三元操作符用於替代下面的代碼:

  ?

1 2 3 4 5 if (val != 0) { return foo(); } else { return bar(); }

  你可以寫成:

   代碼如下:

  return val ? foo() : bar();

  在產生 HTML 程式碼時也是很有用的:

   代碼如下:

  var html = '';

  三、&& 和 ||

  二元布爾操作符是可短路的, 只有在必要時才會計算到最後一項.

  "||" 被稱作為 'default' 操作符, 因為可以這樣:

  ?

1 2 3 4 5 6 7 8 9 function foo(opt_win) { var win; if (opt_win) { win = opt_win; } else { win = window; } // ... }

  你可以使用它來簡化上面的代碼:

  ?

1 2 3 4 function foo(opt_win) { var win = opt_win || window; // ... }

  "&&" 也可簡短代碼.比如:

  ?

1 2 3 4 5 6 7 if (node) { if (node.kids) { if (node.kids[index]) { foo(node.kids[index]); } } }

  你可以像這樣來使用:

  ?

1 2 3 if (node && node.kids && node.kids[index]) { foo(node.kids[index]); }

  或者:

  ?

1 2 3 4 var kid = node && node.kids && node.kids[index]; if (kid) { foo(kid); }

  不過這樣就有點兒過頭了:

   代碼如下:

  node && node.kids && node.kids[index] && foo(node.kids[index]);

  四、使用 join() 來建立字串

  通常是這樣使用的:

  ?

1 2 3 4 5 6 7 8 9 10 function listHtml(items) { var html = ''; for (var i = 0; i < items.length; ++i) { if(i > 0) { html += ', '; } html += itemHtml(items[i]); } html +=''; return html; }

  但這樣在 IE 下非常慢, 可以用下面的方式:

  ?

1 2 3 4 5 6 7 function listHtml(items) { var html = []; for (var i = 0; i < items.length; ++i) { html[i] = itemHtml(items[i]); } return '' + html.join(', ') + ''; }

  你也可以是用數組作為字串構造器, 然後通過 myArray.join('') 轉換成字串.不過由於賦值操作快於數組的 push(), 所以盡量使用賦值操作.

  五、遍曆 Node List

  Node lists 是通過給節點迭代器加一個過濾器來實現的. 這表示擷取他的屬性, 如 length 的時間複雜度為 O(n),通過 length 來遍曆整個列表需要 O(n^2).

  ?

1 2 3 4 var paragraphs = document.getElementsByTagName_r('p'); for (var i = 0; i < paragraphs.length; i++) { doSomething(paragraphs[i]); }

  這樣做會更好:

  ?

1 2 3 4 var paragraphs = document.getElementsByTagName_r('p'); for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) { doSomething(paragraph); }

  這種方法對所有的 collections 和數組(只要數組不包含 falsy 值) 都適用.

  在上面的例子中, 也可以通過 firstChild 和 nextSibling 來遍曆孩子節點.

  ?

1 2 3 4 var parentNode = document.getElementByIdx_x('foo'); for (var child = parentNode.firstChild; child; child = child.nextSibling) { doSomething(child); }

  希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.