1

You can have statements on the right hand of an assignment?

thing: 123123

thing: "asdfasdf"

thing: {asdf:1234}

thing: (function () {
 return 1
})()

parent: for (var i=0; i<10; i++) {
  var a = 0
  console.log(a)
  child: while(a < 5) {
    a++
    if (a > 2)
      break parent
  }
}

http://jsfiddle.net/td951gc7/1/

CheapSteaks
  • 4,821
  • 3
  • 31
  • 48

3 Answers3

2

You are using labels, not assignments

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");
       }
}

See also: How can I use goto in Javascript?

Community
  • 1
  • 1
dave
  • 62,300
  • 5
  • 72
  • 93
  • Where did `goto` come from? ECMA 262 says that there is no goto statement. Is it new in ECMAScript 6 or 7 or something? – Quentin Apr 02 '15 at 21:26
  • sorry, forgot, goto is not a statement. you can use "continue" with a label, which makes it act similar to a goto – dave Apr 02 '15 at 21:35
2

thing: and parent: are not assignments. They are labels (which are a little like targets for GOTOs and are generally only useful when you are dealing with nested loops and want to break or continue on a specific one of those loops).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

So you have good answers to WHY this is so - they are labels - but if you would want to have them like assignments, you could only wrap them in an "object":

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();

You can play around here: http://jsfiddle.net/td951gc7/2/

hexerei software
  • 3,100
  • 2
  • 15
  • 19