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

相关文章:

  • 2022-12-23
  • 2021-11-20
  • 2021-11-23
  • 2022-02-28
  • 2022-02-08
  • 2021-08-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-30
  • 2021-11-18
  • 2021-09-19
  • 2022-02-17
  • 2022-12-23
相关资源
相似解决方案