Question

var Tweet = Backbone.Model.extend({
  defaults: function()
  {
       return
       {
         author: ''
         status: ''
       }
  }
});

according to the syntax it should be

 author: '',
 status: ''

But this provides an error while the one in code working yet producing no output

Was it helpful?

Solution

You have fallen into a trap called automatic semicolon insertion. Specifically, there must not be a newline between return and {

var Tweet = Backbone.Model.extend({
    defaults: function() {
        return { // !!!
            author: '',
            status: ''
        }; // as a convention, I add ; always here, though not strictly needed.
    }
});

Otherwise Javascript considers this being 2 statements:

return;

which just returns the value undefined, and

{
   author: '';
   status: '';
}

which is a block compound statement, where there are 2 labels: author: and status:, each followed by a no-op string literal expression statement ''. Adding a , after the author: '' line makes it a syntax error, as a statement cannot end in a comma / the comma cannot be followed by a label.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top