zoukankan      html  css  js  c++  java
  • JavaScript Rules2

    for-in loops are often incorrectly used to loop over the elements in an Array. This is however very error prone because it does not loop from 0 to length - 1 but over all the present keys in the object and its prototype chain. Here are a few cases where it fails:

    function printArray(arr) {
      for (var key in arr) {
        print(arr[key]);
      }
    }
    
    printArray([0,1,2,3]);  // This works.
    
    var a = new Array(10);
    printArray(a);  // This is wrong.
    
    a = document.getElementsByTagName('*');
    printArray(a);  // This is wrong.
    
    a = [0,1,2,3];
    a.buhu = 'wine';
    printArray(a);  // This is wrong again.
    
    a = new Array;
    a[3] = 3;
    printArray(a);  // This is wrong again.

    Always use normal for loops when using arrays.

    function printArray(arr) {
      var l = arr.length;
      for (var i = 0; i < l; i++) {
        print(arr[i]);
      }
    }

    The following are all false in boolean expressions:

    • null
    • undefined
    • ''the empty string
    • 0 the number

    But be careful, because these are all true:

    • '0'the string
    • []the empty array
    • {} the empty object

    This means that instead of this:

    while (x != null) {

    you can write this shorter code (as long as you don't expect x to be 0, or the empty string, or false):

    while (x) {
    
    
    
    
    
    
    

    And if you want to check a string to see if it is null or empty, you could do this:

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

    But this is shorter and nicer:

    if (y) {

    Caution: There are many unintuitive things about boolean expressions. Here are some of them:

    • 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

    Conditional (Ternary) Operator (?:)

    Instead of this:

    if (val != 0) {
      return foo();
    } else {
      return bar();
    }

    you can write this:

    return val ? foo() : bar();
    
    
    
    
    
    
    

    It is common to see this:

    function listHtml(items) {
      var html = '<div class="foo">';
      for (var i = 0; i < items.length; ++i) {
        if (i > 0) {
          html += ', ';
        }
        html += itemHtml(items[i]);
      }
      html += '</div>';
      return html;
    }

    but this is slow in Internet Explorer, so it is better to do this:

    function listHtml(items) {
      var html = [];
      for (var i = 0; i < items.length; ++i) {
        html[i] = itemHtml(items[i]);
      }
      return '<div class="foo">' + html.join(', ') + '</div>';
    }

    You can also use an array as a stringbuilder, and convert it into a string with myArray.join(''). Note that since assigning values to an array is faster than using push() you should use assignment where possible.

    Node lists are often implemented as node iterators with a filter. This means that getting a property like length is O(n), and iterating over the list by re-checking the length will be O(n^2).

    var paragraphs = document.getElementsByTagName('p');
    for (var i = 0; i < paragraphs.length; i++) {
      doSomething(paragraphs[i]);
    }

    It is better to do this instead:

    var paragraphs = document.getElementsByTagName('p');
    for (var i = 0, paragraph; paragraph = paragraphs[i]; i++) {
      doSomething(paragraph);
    }

    This works well for all collections and arrays as long as the array does not contain things that are treated as boolean false.

    In cases where you are iterating over the childNodes you can also use the firstChild and nextSibling properties.

    var parentNode = document.getElementById('foo');
    for (var child = parentNode.firstChild; child; child = child.nextSibling) {
      doSomething(child);
    }
  • 相关阅读:
    HDU 1213 How Many Tables 并查集 寻找不同集合的个数
    哈哈哈哈哈
    P2251 质量检测(ST表)
    poj3264Balanced Lineup(倍增ST表)
    bzoj1088扫雷(搜索)
    P2258 子矩阵(dp)
    codevs1369 xth 砍树(线段树)
    5.3QBXT模拟赛
    codevs1690 开关灯(线段树)
    zhw大神线段树姿势
  • 原文地址:https://www.cnblogs.com/ly7454/p/2724594.html
Copyright © 2011-2022 走看看