JavaScript SyntaxError: invalid property id

javascript

Solution

You have a syntax error:

var foo = {
    func1:function() {
        function test() {
            alert("123");
        }();
//       ^ You can't invoke a function declaration
        alert("456");
    },
    myVar : 'local'
};

Assuming you wanted an immediately-invoked function, you'll have to make that function parse as an expression instead:

var foo = {
    func1:function() {
        (function test() {
//      ^ Wrapping parens cause this to be parsed as a function expression
            alert("123");
        }());
        alert("456");
    },
    myVar : 'local'
};

Problem

I am trying to execute the following JS code; ``` var foo = {   func1:function(){ function test() { alert("123"); }();     alert("456");   }, myVar : 'local' }; ``` But I am getting an error SyntaxError: invalid property id What is wrong with the above code?

Original source