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?
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'
};
wrap with ()
:
(function test(){
alert("123");
}());
Or:
(function test(){
alert("123");
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With