Thursday 15 May 2014

coding style - Providing a default string parameter in JavaScript -


I have always used the following syntax to ensure that the input variable is not null
  function F (input) {if (input === blank) input = ""; ...}   

Recently, I have seen that it is low to express it as follows.

  function f (input) {input = input? Input: ""; ...}   

But also I have seen this syntax.

  function f (input) {input = input || ""; ...}   
  1. Is not those equivalent ( what is in but how they do)?
  2. Most recommended (readability etc)?

    Note that I will strictly work with the input of the wires as if it is either valid or empty). If I want to expand security to include other types, then should I consider additional issues? "post-text" itemprop = "text">

    First of all, note that they are not all the same. # 2 and # 3 have exactly the same result: Unless the input is true, set it to empty string # 1 means "until input Is null , set it to an empty string, so if false was provided, false false , But # 2 and # 3 will have '' .

    If nothing is not provided # 1, Code> F () , Input will be undefined , because it is the value Which passes when passed, not null

      if (input === undefined) if (input == zero)  
      input = input. ""   

    This is my favorite style. It can be yours, it can not happen. But, at the top, be consistent . < / Div>

No comments:

Post a Comment