[removed] is this a conditional assignment?

后端 未结 4 697
猫巷女王i
猫巷女王i 2021-02-05 12:45

From the google analytics tracking code:

var _gaq = _gaq || [];

how does this work?

Is it a conditional variable value assignment? Is i

4条回答
  •  粉色の甜心
    2021-02-05 13:21

    Actually it's not the same as saying:

    if (!_gaq) _gaq = [];
    

    at least not necessarily. Consider this:

    function outer() {
      var _gaq = null;
      function inner() {
        var _gaq = _gaq || [];
        // ...
      }
    
      inner();
      _gaq = 1;
      inner();
    }
    

    When there's a "_gaq" (I hate typing that, by the way) in an outer lexical scope, what you end up with is a new variable in the inner scope. The "if" statement differs in that very important way — there would only be one "_gaq" in that case.

提交回复
热议问题