" mean in javascript array creation?

This question already has an answer here:

  • What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript? 7 answers

  • It checks if _array is defined, otherwise, it assigns an array to it. Basically a "Use existing or assign new" scenario.

    The second line can then run safely since _array is (presumably) an existing array, or a newly created array, courtesy of the first line.


    It means or . In this case you can read it as get _array variable or create new empty array if _array doesn't exist .


    This | character is called a pipe.

    When used in a pair || it represents a logical OR. (It's used widely in other languages too).

    It will try to do the left most expression first, and if that expression evaluates to false, it will do the right most expression.

    In our case, it tests if the variable _array exists, if it does it basically assigns _array to _array . If it does not exist yet, it will initialize _array as an empty array ( [] ).


    It could also be rewritten as a ternary operator like:

    var _array = _array ? _array : [];
    
    链接地址: http://www.djcxy.com/p/95288.html

    上一篇: 'this.x1 = options.x1是什么?

    下一篇: “意味着在JavaScript数组创建?