2

I've been translating a JavaScript library into C# and I came across something really strange to me

The original author had a "constructor" inside a function. It was defined a way I had never seen before:

var name = function(/*parameters*/)
{
    /*Set instance variables here*/
    this.BMAX = 16;   // maximum bit length of any code
    this.N_MAX = 288; // maximum number of codes in any set
    {
         /*Constructor logic here*/

        var a;          // counter for codes of length k
        var c = new Array(this.BMAX+1); // bit length count table
        var el;         // length of EOB code (value 256)
        var f;          // i repeats in table every f entries

        //Etc....
    }
}

Is this common practice to do this? It was the first time I have seen it done like this.

If so, can someone explain why there is an extra set of brackets? Why not just do them all in there?

Is this even "valid"?

3
  • The extra block, from your reduced snippet of code, appears to serve no purpose whatsoever. Commented Sep 29, 2014 at 10:11
  • I could post the full code, but it would be to large... Its a compression library Commented Sep 29, 2014 at 10:12
  • 2
    You should remove 90% of those variables. Leave enough to get the point across ;-) Commented Sep 29, 2014 at 10:16

3 Answers 3

3

There is absolutely no point in adding extra brackets like that in the code.

Functionally, it adds nothing. It's just a visual separation that should've been done in comments.

I can see this causing problems if the code is minified, even:

var a = 1
{
    var b = 2
}

VS:

var a = 1{var b = 2}
// SyntaxError: Unexpected token {
Sign up to request clarification or add additional context in comments.

5 Comments

This is true, however it will only be a problem if you don't use (the optional) semicolon at the end of each line.
@Snellface: does the runtime not add semicolon for you?
No it does not, but its not a problem for me since my spine adds them for me :P. Not everyone uses an IDE that adds semicolons. And since they are optional people might not see why you should use them.
Note to prev comment: This is of course an opinion, there might be reasons why not to use them, say like saving a byte (or two, depending on your encoding) per line of code.. but then again, you could use only semicolons instead of newlines and that could save you even more because some systems use 2 chars per newline instead of only one (\r\n vs \n for those who are interested but does not know of this, and it depends on your application and/or your operating system)
@Prabhu: I just assumed you were talking about the development runtime, if you were talking about the javascript engine running in your browser of choice then the answer is no, the javascript engine will not add semicolons to your javascript because it can parse it either way. If you were talking about the minifier runtime then it will depend on your minifier, in the perfect of worlds they will add semicolons for you, but it really depends :P
3

It is valid yes, i would say that have done so only to separate the constructor logic from the other code visually as any variables declared inside a scope like that is still visible outside of the brackets (as long as you are in the same function).

It serves no functional purpose

My dirty secret: I sometimes do like that with javascript to make Visual Studio auto indent code for me.

2 Comments

Use to use VS. Moved to WebStorm, Sublime and MonoDevelop, Linux FTW
I really like VS, used it for too long to really wanna look at other IDEs, but since you sound like you have found the the light in your new tools would you like to share with me what makes them so great? (Who knows, some day i might have to use linux as my primary work platform and the better tools i have the less bothered i will be by it (read: been using microsoft products too long, too stupid to find new tools))
2

Ok I'm late to drop an answer and the answers are already great, but don't really explain what the {} statement actually is.

It's called statement block describing what is called a compound statement which means it allows you to add multiple substatements wherever javascript expects a single substatement.

You've probably seen this already

if(foo) 
    doStomething();
else
    doSomethingElse();

and asked yourself? what the hell? Where are those curly braces? It's not a special case. That's just how most statements in javascript look like, they usually expect a single statement per default. By using statement block we can expand it to a compound statement containing as many substatements as we want.

Is there an opposite to a statement block? Yes there is, it's called the empty statement ;. It means no statement where a single statement is expected.

for(var i=0; i<100; i++); //empty statement
console.log(i); //Is called once, logs "100"
doSomething(i); //Is called once, with 100 as argument;

This is perfectly valid, even though useless in this case. But that's a way you can also use for loops, it's sometimes shorter and more elegant.

for(var i=0; i<100; i++) //no empty statement
console.log(i); //Is called 100 times, logs 0 - 99
doSomething(i); //Is called once with 100 as argument;

And finally like it's usually written, with the compound statement

for(var i=0; i<100; i++){ //compound statement
    console.log(i); //logs 100 times
    doSomething(i); //gets called 100 times
}

Since the curly braces are also the Object literal it's of course difficult to tell them apart sometimes, but they are completely different things.

Reference

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.