[removed] Why does this return statement cause a syntax error?

前端 未结 2 762
天命终不由人
天命终不由人 2021-01-20 09:08

I\'m using Apatana 3, i modified the JS code formatter a little bit to let it seem more clear, below is the code after format, it give me an error:

    copyO         


        
2条回答
  •  借酒劲吻你
    2021-01-20 09:46

    Haha, this is a classical one;)

    Javasript breaks on

    return
    {
    

    because it treats { as a new block and implicitely inserts a semicolon:

    return;
    {
    

    thus returning undefined:-D

    The Problem is Javasript inserting a Semicolon at the end of a line when the statement makes sense. Since return can stand on it's own, Javascript interprets it as a complete statement and inserts the semicolon, thus breaking your code.

    Actually, this is the reason, why in Javascript you alway should avoid those newlines and write:

    copyOffset : function( index ){
        return{
            x : index, y : index
        };
    }
    

提交回复
热议问题