如果使用对象样式向函数添加属性会发生什么?

J.J*_*Joe 3 javascript

奇怪的是,以下代码在最新的Chrome浏览器中没有出现任何错误:

function func(){
    x:1
}
func();
Run Code Online (Sandbox Code Playgroud)

究竟发生了什么?(我不是试图在函数中添加属性,我知道如何使用func.property或者arguments.callee.property)

更新:如果x:1labeled statement,那么

function func(){
    1
}
Run Code Online (Sandbox Code Playgroud)

被认为是有效的功能.这是1做什么的?它被忽略了吗?这是陈述,表达还是其他什么?

Nin*_*olz 6

你的路线

x:1
Run Code Online (Sandbox Code Playgroud)

是一个带标签的声明.

标签的语句可以与breakcontinue语句一起使用.它为带有标识符的语句添加前缀,您可以参考该标识符.

示例:

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);
}
Run Code Online (Sandbox Code Playgroud)