3

Strangely, the following code gives no error in the lastest Chrome browser:

function func(){
    x:1
}
func();

What happened exactly? (I am not trying to add a property to the function, I know how to do that using func.property or arguments.callee.property)

Update: If x:1 is a labeled statement, then

function func(){
    1
}

is considered a valid function. What is 1 here doing? Is it just ignored? Is it a statement, expression or something else?

J.Joe
  • 644
  • 2
  • 7
  • 20
  • Your title conflicts with the body of your post. `"What happens if you add a property..."` - `"I am not trying to add a property..."`. Perhaps a title referencing a question about this specific syntax would be more appropriate. – Lix May 24 '16 at 08:22
  • The later function is valid because a statement with just a number is valid. – evolutionxbox May 24 '16 at 08:39

1 Answers1

6

Your line

x:1

is a labeled statement.

The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.

Example:

var i = 0;
var j = 8;

checkiandj: while (i < 4) {
    console.log("i: " + i);
    i += 1;

    checkj: while (j > 4) {
        console.log("j: " + j);
        j -= 1;

        if ((j % 2) == 0)
            continue checkj;
        console.log(j + " is odd.");
    }
    console.log("i = " + i);
    console.log("j = " + j);
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392