SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 29 of 29
  1. #26
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, you guys!

  2. #27
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    When the operand is undefined, if(aboutSetupOne) is still falsy - there's no need to define the variable as being null.
    If you do that in the global scope as per the example in question, an undefined operand generates an error unless you explicitly state its parent object.
    Code:
    if( window.aboutSetupOne )
     ...
    With:

    Code:
    aboutSetupOne = localStorage && localStorage.getItem( 'aboutSetupOne' );
    JavaScript checks the first operand, and only if it's truthy does JavaScript then check the second operand. No errors are generated.
    This is why in JavaScript the && operator is also called the "guard operator".
    You can do that with undefined operands using || not with &&. Why don't you try the example I gave for the purpose, which will always be undefined?

    Please try to not bring inappropriate practices from other programming languages in to your JavaScript coding.
    English spoken with a French accept can be quite wonderful. JavaScript written with influences from other languages isn't.
    Just what do you consider to be an inappropriate practice?
    Tab-indentation is a crime against humanity.

  3. #28
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Code:
    aboutSetupOne = _localStorage && _localStorage.getItem( 'aboutSetupOne' );  //ERROR
    Kudo's, just ran you code and it definitely throws an error. And using window._localStorage && window._localStorage.getItem('aboutSetupOne'); definitely fixes it too.

  4. #29
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    If you do that in the global scope as per the example in question, an undefined operand generates an error unless you explicitly state its parent object.
    Code:
    if( window.aboutSetupOne )
     ...
    Ahh, good catch. This is also why doing things in the global scope with global variables is not a good idea.

    If a variable such as localStorage is intended to be accessed globally, it's best to make it explicit, for example by using window.localStorage
    After looking at the Local Storage documentation, I see that the compatibility code that they have there uses window.localStorage too, which helps to add further weight to it being the more effective technique to use.

    Quote Originally Posted by Logic Ali View Post
    Just what do you consider to be an inappropriate practice?
    Many times people come across to programming in JavaScript from a background of Java, or PHP, or C++, where those different environments have practices that can be simulated by JavaScript, but also have other more efficient ways of being done.

    This is where following certain code conventions can help, as well as reading up on books like JavaScript: The Good Parts
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •