Sergino
Sergino

Reputation: 10838

Javascript errors in simple javascript code

I have pretty simple JS

function () {          //**Expected identifier** over first bracket
    window.Root = {
        Models : {},
        Views : {},
        Comments : {}

    }
}
.call(this) //**Syntax error** over dot

But getting an errors

function () - Expected identifier

. call(this) - Syntax error

Could some one explain why that errors talking place and how to fix that?

Upvotes: 0

Views: 55

Answers (2)

Qantas 94 Heavy
Qantas 94 Heavy

Reputation: 16020

You've forgotten the parentheses:

(function () {
    window.Root = {
        Models: {},
        Views: {},
        Comments: {}
    }
}).call(this);

Because expressions cannot start with function or {, it is treated as a declaration and therefore fails. Function declarations must have an identifier, and since .call is completely separate from the function declaration, it is therefore a syntax error (as .call(this) in itself is not a valid statement).

Upvotes: 2

MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

You have missed the paranthesis in your function.

(function () {          //**Expected identifier** over first bracket
window.Root = {
    Models : {},
    Views : {},
    Comments : {}

}
});
.call(this) //**Syntax error** over dot

Upvotes: 0

Related Questions