What does the Javascript expression 'a = a || function() {…}' mean?

前端 未结 4 1342
执念已碎
执念已碎 2020-12-28 17:53

I\'m not sure what this construct means but I\'ve seen it a few times. The example below is from another Stack Overflow question. I\'m not sure how to interpret the initial

4条回答
  •  执笔经年
    2020-12-28 18:18

    The || basically means: If Object.keys is not defined, define it using the expression behind the ||.

    This behavior bases on the JavaScript feature that any variable that is undefined evaluates to false. If the variable is true, the second expression does not need to be evaluated, if it is false it does.

提交回复
热议问题