2015-04-02 78 views

回答

2

您正在使用的标签,而不是分配

i = 0; 
a: while(i < 2) { 
     j = 0; 
     i = (i || 0) +1; 
     console.log('hi'); 
     while(j < 2) { 
      j++; 
      console.log("there"); 
      if (i < 1) continue a; 
      console.log("hello"); 
     } 
} 

参见:How can I use goto in Javascript?

+0

'goto'从哪里来的? ECMA 262表示没有goto声明。它在ECMAScript 6或7中是新的还是什么? – Quentin 2015-04-02 21:26:11

+0

抱歉,忘记了,goto不是一个声明。你可以使用“继续”的标签,这使得它的行为类似于goto – dave 2015-04-02 21:35:11

1

所以,你有很好的答案,为什么会是这样 - 他们是标签 - 但如果你想有他们喜欢的任务,你可以只包起来的“对象”:

var myClass = { 
    thing1: 123123, 
    thing2: "asdfasdf", 
    thing3: {asdf:1234}, 
    thing4: function() { 
    return 1 
    }, 
    parent: function() { 
     for (var i=0; i<10; i++) { 
     var a = 0 
     child: while(a < 5) { 
     a++ 
     console.log(a) 
     if (a > 2) 
      break 
     } 
    } 
    } 
}; 

console.log(myClass.thing1); 
console.log(myClass.thing2); 
console.log(myClass.thing3['asdf']); 
console.log(myClass.thing4()); 

myClass.parent(); 

你可以在这里玩: http://jsfiddle.net/td951gc7/2/

相关问题