{} idiom in Javascript work?

From this question: What does "var FOO = FOO || {}" (assign a variable or an empty object to that variable) mean in Javascript?

I've learned that var FOO = FOO || {} var FOO = FOO || {} essentially means "If FOO exists, then leave it untouched, else make it an empty object".

But how?

This is how I would parse this syntax:

var FOO = (FOO || {})

So: If FOO exists AND evaluates to Boolean value of True, then (FOO || {}) will return True, so eventually FOO will be completely overwritten and will hold the Boolean value of True from now on.

Else (FOO || {}) will return to whatever Boolean value {} evalueates to. Since an empty object, which {} is, always evaluates to True...

Then in ANY case (FOO || {}) should evaluate to True, so...

In ANY POSSIBLE CASE, after evaluating var FOO = FOO || {} var FOO = FOO || {} , FOO should hold the trivial Boolean value of True, regardless of whatever it was holding before. Essentially, to my understanding, var FOO = FOO || {} var FOO = FOO || {} should be equivalent to var FOO = True .

Where is my mistake?


If FOO exists AND evaluates to Boolean value of True, then (FOO || {}) will return True

That isn't how the || operator works in JS.

A correct interpretation is:

If the left hand side is a true value, evaluate as the left hand side (ie FOO ), otherwise evaluate as the right hand side (ie {} ).

var zero = 0;
var one = 1;
var two = 2;

console.log(zero || two);
console.log(one || two);

So: If FOO exists AND evaluates to Boolean value of True, then (FOO || {}) will return True, so eventually FOO will be completely overwritten and will hold the Boolean value of True from now on.

That is wrong, but the below lines surprise you if your background is Strictly Typed Languages :)

The expression doesn't return a boolean value. It returns the expression that can be evaluated to true.

Here is the docs for the same

Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.

Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

Different examples given in docs might help you understand the above words.

o4 = false || (3 == 4)   // f || f returns false
o5 = 'Cat' || 'Dog'      // t || t returns "Cat"
o6 = false || 'Cat'      // f || t returns "Cat"
o7 = 'Cat' || false      // t || f returns "Cat"
o8 = ''    || false      // returns false

JavaScript || operator returns the expression itself not the boolean value. Here's a reference from Mozilla documentation

Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true.

Reference :

Conversion to True in boolean depends on whether the expression evaluates to a Truthy value.

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

上一篇: Python NameError:未定义全局名称'assertEqual'

下一篇: Javascript中的成语工作?