How to use goto in javascript? - Stack Overflow

i get reference from here "How can I use goto in Javascript?"i understand the code as below[l

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(){}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745436705a4627639.html

相关推荐

  • How to use goto in javascript? - Stack Overflow

    i get reference from here "How can I use goto in Javascript?"i understand the code as below[l

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信