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

后端 未结 2 625
执笔经年
执笔经年 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...)

提交回复
热议问题