JavaScript curly braces with no function or json

后端 未结 6 821
别那么骄傲
别那么骄傲 2020-12-06 16:16

Just opened a client\'s javascript file and the first lines are along the lines of this:

{
    var s_account=\"blog\";
}

Which I don\'t get

6条回答
  •  忘掉有多难
    2020-12-06 17:19

    Can anyone tell me why there are curly braces with no text before them or after them? What does it do / what is the point of it?

    There is no significant point to them in Javascript. It will act exactly the same as if the code was just

    var  s_account="blog";
    
    Speculation

    In other languages which have block scope, this might restrict the scope of the variable, but since JS doesn't have that feature (for better or worse), braces without a control structure or function are essentially meaningless and ignored.

    Most likely this code was left over from a deleted function or if statement however. It definitely is not a pattern to be copied.

提交回复
热议问题