How are labels used with statements that are not a loop?

后端 未结 2 607
执笔经年
执笔经年 2021-01-02 10:41

According to the ECMAScript 5.1 spec, section 12.12, any statement can be labelled - and in a brief test my browser accepted a label before any statement. The spec also stat

相关标签:
2条回答
  • 2021-01-02 10:49

    Apparently the break and continue statements can be used within any statement:

    http://docstore.mik.ua/orelly/webprog/jscript/ch06_11.htm

    In which case things like this become legal:

    function show_alert()
    {
        label:
        {
            break label;
            alert("Hello! I am an alert box!");
        }
        alert("hi");
    }
    

    When show_alert() is called, only the "hi" alert is shown.

    As far as I know, this is the only use of the {} code blocks, other than for code styling. (there was a question on here about that, and noone could come up with anything other than readability, but I can't find it now...)

    0 讨论(0)
  • 2021-01-02 11:01

    Yes you can label any statement. You just need to put the statement in curly braces, i.e.

    {start:var a=1;}
    

    this will not show undefined label error.

    0 讨论(0)
提交回复
热议问题