Blog Post default image

Ran into this little problem the other day when using someone else’s specific syntax. Returning a complex object using a notation that puts objects brackets onto new lines was throwing an error. I sat there staring at it for maybe like 20 minutes wondering what the hell I’d been drinking recently and why I couldn’t trace the error.

Turns out it was a very simple syntax pit fall I hadn’t come across before, because I’m a Person A in the example below with regards to formatting my scripts.

// Person A's format
function() {
 // function bracket on same line as declaration 
}

// Person B's format
function() 
{
 // function bracket on new line 
}

You can argue which is better all you want but likely you will run into both forms as a developer, both have their merits, and neither saves compile time.

So if you are a person B or just forced to use that format when defining objects, there is a catch you should know about.

If you do this

// Object brackets on new line under declarations
function() 
{
    return 
    {
        x: null,
        y: null
    }
}

You will get an error, specifically it relates to the return. You can not put your return object on a new line. Instead you need to return an object like this.


function() 
{
    // Notice where the bracket is now
    return {
        x: null,
        y: null
    }
}

So it’s a good thing to keep in mind when using return, if you are a fan of brackets getting new lines 🙂

Enjoy!

Comments are closed.