JavaScript open brace in the same line

后端 未结 6 1576
心在旅途
心在旅途 2020-12-16 03:50

I remember there is a convention/recommendation to put opening brace in the same line, because of the way JavaScript adds a semicolon or something.

//OK
fun         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-16 04:32

    There is no issue with declaring functions, but you can get into trouble when returning objects:

    function foo()
    { // this is OK
    
        return
        { // this is BAD!
            name: "bletch"
        };
        // actually returns undefined!
    }
    

    A semi-colon is automatically inserted after the return statement, and that will break your code.

提交回复
热议问题