2

I have:

var doBlah = function(param0) {

  return {
    objectMember: function( param1 )
    {
        var a  = param1;
    }
  }
}

Which works fine but when I do a different indentation as:

var doBlah = function(param0) {

  return
  {
    objectMember: function( param1 )
    {
        var a  = param1;
    }
  }
}

I get the following error: Uncaught SyntaxError: Unexpected token (

Why is this? Seems to be behaving similar to python. Any references to official docs would be greatly appreciated.

Robert
  • 10,126
  • 19
  • 78
  • 130
  • 1
    Javascript attempts to insert missing semicolons for you, so `return` followed by a newline is often "corrected" to `return;`. See [this answer](http://stackoverflow.com/questions/18221963/javascript-function-fails-to-return-object-when-there-is-a-line-break-between-th). – fzzfzzfzz Mar 10 '15 at 00:56

1 Answers1

3

It is because in js the new line can be considered as end of an statement so your code will look like

var doBlah = function(param0) {

  return ;//this is ended here
  { //here you have block definition starting
    objectMember: function( param1 )//now you have an invalid syntax here
    {
        var a  = param1;
    }
  }
}

So

var doBlah = function(param0) {

  return {
    objectMember: function( param1 )
    {
        var a  = param1;
    }
  }
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531