最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use goto in javascript? - Stack Overflow

programmeradmin2浏览0评论

i get reference from here "How can I use goto in Javascript?"

i understand the code as below

[lbl] first:
alert("Test")
goto first;

however. why the code below does not work for me

goto end;
alert("skipped line");
[lbl] end:

when I run the above mand I will get an error like this

i get reference from here "How can I use goto in Javascript?"

i understand the code as below

[lbl] first:
alert("Test")
goto first;

however. why the code below does not work for me

goto end;
alert("skipped line");
[lbl] end:

when I run the above mand I will get an error like this

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Jun 24, 2015 at 2:27 Error PersonError Person 331 gold badge1 silver badge4 bronze badges 6
  • Because end is not a label. Labels in JavaScript have the form label: statement (granted the preprocessing tool might be able to handle that case, but apparently it does not). – Felix Kling Commented Jun 24, 2015 at 2:28
  • [lbl] end: ; might work. end: is the label and ; is the empty statement. – Felix Kling Commented Jun 24, 2015 at 2:35
  • sorry i misstype. i update my questions – Error Person Commented Jun 24, 2015 at 2:39
  • 3 Well the obvious ment here is to never use goto in the first place. It's considered an evil construct by many. Instead, use conditionals, loops, functions, methods and return statements to construct your flow. – jfriend00 Commented Jun 24, 2015 at 2:44
  • i still not understand what evil? :v – Error Person Commented Jun 24, 2015 at 2:53
 |  Show 1 more ment

1 Answer 1

Reset to default 2

Labels are for loops and blocks.

Loop usage:

var allPass = true
top:
for(var i=0; i < items.length; ++i)
    for(var j=0; j < tests.length; ++j)
        if(!tests[j].pass(items[i])){
            allPass = false
            break top
        }

You can also use continue label.

Block usage:

foo: {
    console.log("face")
    break foo
    console.log("this will not be executed")
}
console.log("swap")

Non-strict, non-generator, function usage:

L: function F(){}
发布评论

评论列表(0)

  1. 暂无评论