The difference between the options `var options = options || {} `and` options || (options = {}) `

I often use var options = options || {} var options = options || {} as the default method for an empty object. It is often used to initialize an option object if it is not passed in the function call parameter.

The fact is that I read in several places (blog posts, source code) that options || (options = {}) options || (options = {}) it’s better to express the intent of the developer. Can anyone clarify this? I do not see a functional difference between the two, so there is something missing for me.

--- change

I saw in the source code Backbone.js in several places, for example https://github.com/documentcloud/backbone/blob/0.9.2/backbone.js#L273

I think I also saw this in jQuery source code. And in multiple Js writing style manuals that flourished.

--- change code 2:

 var func = function(param, options) { // How I do it var options = options || {}; // How I should do it in the "same" way options = options || {}; // The "other" way options || (options = {}); } 
+6
source share
7 answers

They should do the same, but there is a better way.

Theoretically, the second, assigning only if the value is false, can eliminate the assignment and be faster. Indeed, in jsperf we see that it is (12%).

In fact, the explicit if statement executes as quickly as the then-assign condition:

 if(!options) options = {}; 

Try the test on your browser / machine .

I think it is explicit, if it is most clear, and has no punishment.

Edit:

If you expect the object to be passed to a function , I think the best test is:

 if(typeof options !== 'object') options = {}; 

This ensures that you have the object later, even if it is empty. Any other test (for undefined or false) will allow a true non-object through a non-zero number or non-empty string. However, as jsperf shows, this is ~ 15% slower. Since you only do this when entering a function that will process objects, I would say that this is a useful compromise, and a little slower than always assigned .

+3
source

There is no real difference, assuming you mean:

 function fn(options) { // type a options = options || {}; // type b options || (options = {}); } 

Basically a matter of preference, I think (a) is much clearer, I do not like the statement without an assignment to LHS.

+5
source

There is no functional difference.

The second construct (subjectively) looks as if it does more than the first construct.

The argument of the counter is that the first design is a common template, so it’s easier to recognize what to do.

+4
source

There is a functional difference: one uses var and the other does not. If there is any chance that the options variable does not exist in the current area, it is much better to use var rather than the risk of options flowing implicitly into the outer areas.

If the parameters are guaranteed to exist (for example, inside a function whose parameters include options ), the two operators are functionally identical, so the problem boils down to the relative stylistic merits of options = options || {} options = options || {} compared to options || (options = {}) options || (options = {}) . Personally, I see a slight difference: both require the same knowledge about how the JavaScript || operator works , therefore, as soon as you remove this coefficient from the equation, I would prefer options = options || {} options = options || {} as a little more readable because it was shorter and simpler. In any case, the intent of the developer seems the same to me.

+2
source

In reality, there is no functional difference between the two, although their mechanics are not identical.

The first form sets the local options variable to the options parameter or to an empty object if the argument has a false value (for example, if it was not provided).

The second form evaluates the value of the options parameter (if it is not false); otherwise, it evaluates the result of assigning an empty object to this parameter. Thus, the difference from the first form is that if options is true, execution is not performed.

Personally, I think the second form is a less readable version

 if(!options) { options = {}; } 

which is identical both in function and in mechanics.

+1
source

There is no functional difference, the idea is that options || (options = {}); options || (options = {}); closer to what the programmer really wants to express, in fact:

 if (typeof options == "undefined") { options = {}; } 

Operator || used to create shorter code rather than sharper code.

+1
source

Definitely subjective, but I would not use the second.

Reason: I feel that assignments in expressions are somewhere deeper than the upper level = obfuscation.

Some C programmers like these (I once used), they do things like extra parsers to make the assignment more understandable ... making the whole expression unfamiliar. Why bother if you can just be clear?

The most obvious may be:

 if (!options) options = {}; 
0
source

All Articles