Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird JavaScript syntax I've never used before, but it works

Tags:

javascript

Do you know why this code compiles and what is something?

function Box() {
  something: {
    alert(1);
  }
}

var box = new Box();
like image 424
cryss Avatar asked Aug 10 '15 09:08

cryss


2 Answers

That's a javascript label.

It is used to break or continue nested loops.

Here is the doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

// Example code
myLoop:for (var i = 0; i < 10; ++i)
    for (var j = 0; j < 10; ++j)
        if (whatever)
            break myLoop;
like image 132
Eloims Avatar answered Oct 19 '22 06:10

Eloims


That is a label.

They are used in combination with break and continue to choose which loop the statement should apply to when you have nested loops.

It is pointless where it is.

like image 35
Quentin Avatar answered Oct 19 '22 05:10

Quentin