What is the purpose of these extra curly braces?

So, I recently started work at a new place of work, and I came across a javascript format that makes me question its purpose. (in particular, brackets {})

var _occurrences = getOccurrences($('#ddlTours').val()); { var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID); { _occurrenceID = _occurrence.occurrenceID; } } 

For me, it is almost like trying to build an object. i.e.

 var _occurrences : // Ignoring = getOccurrences($('#ddlTours').val()); { _occurrence : // Ignoring getObjectByValue(_occurrences, 'tourID', booking.tourID); { _occurrenceID : _occurrence.occurrenceID; } } 

But, as I understand it, he will execute it as.

 var _occurrences = getOccurrences($('#ddlTours').val()); var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID); _occurrenceID = _occurrence.occurrenceID; 

Or is it so that _occurrence gets delete and does not sit around like its encapsulated, and we assign var, which is outside the encapsulation. Does this really work as a performance improvement? i.e.

 Global var a = 1 { b = someFunction() // After execution because of encapsulation it poofs??? for(var c in b) { a += c.somefunction() } } 

Another option is just bad code?

Or perhaps this is meant as a logical separation of the code to help dev?

I'm just wondering if anyone can shed some light on this for me :)

+46
javascript
May 19 '15 at 5:41
source share
5 answers

You are right to question these braces. They do nothing. The code inside the curly braces is executed as if the brackets were not there. It is clearly a mistake to make them like that.

As you already mentioned, it looks like someone might think that curly braces will enter the scope of the block, possibly forcing the variable to leave the scope after closing the curly braces. But JavaScript does not have the ability to block var variables! (It has a block scope for let , but only on new JavaScript machines that support let .)

Or maybe they just thought it was a good way to document where variables are used. This is not true.

Adding to the humor here, the code seems to be missing a var for the whole _occurrenceID , so it probably creates a global variable inadvertently!

The way you rewrote the code without curly braces is really the way it will be executed. This is the best idea of ​​what the code actually does, and how to write code. (Fixed the missing var , of course ...)

+45
May 19 '15 at 5:53 a.m.
source share

Or perhaps this is meant as a logical separation of the code to help dev?

I am going to make 2 assumptions here:

  • the developer was not incompetent
  • you left a lot of lines between the knees

(if 1 is incorrect, then all bets are disabled)

Blocks with rounded Do-nothing blocks have no purpose - in various text editors they mark sections that can be collapsed and thus removed from view. I often do this if I have 50 lines that I know, but I have to constantly scroll through the past. Put curls around the contents (remember to nest), click the minimize / collapse icon in the ditch β†’ the code will disappear. My special editor will remember the folded blocks, so I don’t need to re-stack every time.

+10
May 19 '15 at 15:04
source share

As far as I can tell, curly braces are just a hint to a developer who reads code.

My guess is that curly braces and nesting are just to illustrate that the list contains an element containing an identifier. This can help the reader understand that if they are editing any of this code or moving it, then also edit the internal code to fit.

+8
May 19 '15 at 5:54
source share

These brackets are designed to make the code more readable, as some programmers think. Brackets have nothing to do with any other functionality. People make the code connected. Welcomes a new curious experience. :-P

+3
May 19 '15 at 5:57
source share

See code with compiler view.

Sliding brackets are just a scope definition. Not more.

Consider this example.

 for(var c in b) { a += c.somefunction(); } 

This code is similar to

 for(var c in b) a += c.somefunction(); 

i.e. by default, the loop has scope until the next statement.

Similarly

 var _occurrences = getOccurrences($('#ddlTours').val()); { var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID); { _occurrenceID = _occurrence.occurrenceID; } } 

It is simply defining a region and adding operators within that region. Despite the fact that you declare a variable in this area, the concept of "Rise" will turn the entire variable into the top one.

A variable declaration within scope is:

 if(true){ var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID); } 

.

In simple words

 var _occurrences = getOccurrences($('#ddlTours').val()); { var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID); { _occurrenceID = _occurrence.occurrenceID; } } 

equally:

 var _occurrences = getOccurrences($('#ddlTours').val()); if(true){ var _occurrence = getObjectByValue(_occurrences, 'tourID', booking.tourID); if(true){ _occurrenceID = _occurrence.occurrenceID; } } 
+1
May 20 '15 at 11:37
source share



All Articles