0
 var titleModel = function () {
    this.title = ko.observable("test");
    this.desc = ko.observable("test");
    return
    {
        title: this.title,
        desc: this.desc
    }
}

The above code is a model where i want to give access only title,desc from return function only. However, it says a ';' is required. I think the syntax is correct.

However, if i remove the return all works correctly.

I would appreciate if anyone could tell me what the issue is.

user799329
  • 39
  • 5

2 Answers2

3

You need to return the object on the same line. return is strange like that.

var titleModel = function () {
    this.title = ko.observable("test");
    this.desc = ko.observable("test");
    return {
        title: this.title,
        desc: this.desc
    }; // semicolon here.
}; // semicolon here.
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • No, it's not that `return` is strange - it's that JavaScript is strange and adds `;` for you on every new line where it can. In this case it can as it would still be valid code. – Chris Jun 08 '14 at 20:55
1

Move the starting brace on the same line as the return.

return { ..
...
}

If not the return is considered as a separate line. This is because the terminating semicolon is optional in javascript (What are the rules for Javascript's automatic semicolon insertion (ASI)?).

source.rar
  • 8,002
  • 10
  • 50
  • 82