Javascript概念

  1. 判断属性是否存在或有值

    // BAD: This will cause an error in code when foo is undefined 
    if (foo) { 
      doSomething(); 
    } 
    // GOOD: This doesn't cause any errors. However, even when 
    // foo is set to NULL or false, the condition validates as true 
    if (typeof foo != "undefined") { 
      doSomething(); 
    } 
    // BETTER: This doesn't cause any errors and in addition 
    // values NULL or false won't validate as true 
    if (window.foo) { 
      doSomething(); 
    } 
    // UGLY: we have to proof existence of every 
    // object before we can be sure property actually exists 
    if (window.oFoo && oFoo.oBar && oFoo.oBar.baz) { 
      doSomething(); 
    }
  2. 1

posted @ 2012-03-14 21:55    阅读(201)  评论(0编辑  收藏  举报