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

前端 未结 7 1964
野性不改
野性不改 2020-11-22 02:07

Looking at an online source code I came across this at the top of several source files.

var FOO = FOO || {};
FOO.Bar = …;

But I have no ide

7条回答
  •  花落未央
    2020-11-22 02:56

    Notice that in some version of IE this code won't work as expected. Because the var, the variable is redefined and assigned so – if I recall correctly the issue – you'll end up to have always a new object. That should fix the issue:

    var AEROTWIST;
    AEROTWIST = AEROTWIST || {};
    

提交回复
热议问题