I don't understand this code

Possible Duplicate:
What does "var FOO = FOO || {}" mean in Javascript?

I don't understand that :

var gapi = window.gapi||{};

Can you explain me? gapi is a bool?


It means if the variable gapi exists already, and its value does not evaluate to a boolean false one, assign that to the variable gapi otherwise assign a new object to it.

This practice is helpful to avoid overwriting of variables.

These are the JavaScript values that evaluate to boolean false:

  • false
  • undefined
  • null
  • 0
  • NaN
  • the empty string ( "" )

  • The logical or stops if the first symbol evaluates to something different from a truly value, like, false , undefined , null , '' or 0 .. Otherwise it takes the second argument.

    In this case, if gapi is not a global object defined in window, it assigns to gapi the new empty object {} .

    链接地址: http://www.djcxy.com/p/95284.html

    上一篇: 线的含义是什么:var DYN

    下一篇: 我不明白这个代码