I have found a piece of code in my company's project like the following:
while(condition){
code...
reloop: {
if(somethingIsTrue) {
break reloop;
}
}
code...
}
I don't understand what reloop
does, can anyone give a simple explanation?
It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.
the -> int just tells that f() returns an integer (but it doesn't force the function to return an integer). It is called a return annotation, and can be accessed as f. __annotations__['return'] . Python also supports parameter annotations: def f(x: float) -> int: return int(x)
Arguments typically have some restrictions on the expression used for that argument. For example, the input argument to the INDEX function can only be an object name. Depending on the function, one or many data objects can be used for an input argument for one function evaluation.
In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever. With arrow functions the this keyword always represents the object that defined the arrow function. Let us take a look at two examples to understand the difference.
reloop:
is a label
. They are rarely used, and serve a very specific purpose: they let you break
or continue
outer loops from inner loops.
The article on MDN about labels explains this better.
Note that labels are very rarely used, and most of the time needing a label hints that your code is unclear, and should be restructured. I have never, not even once, used a label in javascript
.
Avoid them unless they are truly the only clean solution to piece of code that proves difficult to write. Prefer, instead, splitting code into functions that you can return
from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With