I've recently come across Labeled function declarations
. There is not much said about them on MDN website:
Starting with ECMAScript 2015, labeled function declarations are now standardized for non-strict code in the web compatibility annex of the specification.
L: function F() {}
The standard doesn't add much details either and there's nothing on the web as well.
Has anyone used them? What's the purpose of them? Any information is appreciated.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.
The reason modern compilers give warnings on an attempt to call a function before seeing a declaration is that a declaration allows the compiler to check if arguments are of the expected type.
The function declaration (function statement) defines a function with the specified parameters. You can also define functions using the Function constructor and a function expression.
A function definition requires all parts of the function: a header (which includes the function return type, the function's name, and an argument list) and a function body.
In short, use function declarations when you want to create a function on the global scope and make it available throughout your code. Use function expressions to limit where the function is available, keep your global scope light, and maintain clean syntax. Function declaration, MDN docs.
Starting with ECMAScript 2015, labeled function declarations are now standardized for non-strict code in the web compatibility annex of the specification . In strict mode code, however, this will throw a SyntaxError :
Given below are the examples of the JavaScript Declaration: Function definition with no parameter and function calling with no arguments. Function definition and function calling both does not have parameters and arguments, respectively. Consider number 594.
Another important function performed by labeling is to provide statutory warning required by law. To put ‘smoking is injurious to health’ on the package of cigarette and ‘Chewing Tobacco is Injurious to Health’ on the package of Pan Masala are the examples of statutory warning?
As far as I could gather, the only purpose seems to be support for bad code floating on the web. They cannot be referenced, and are completely useless.
Brendan Eich, 2012:
People tend to make unnecessary labels, especially in event handlers where javascript: at the front of the HTML attribute value has been seen in the wild. Couple that with a function expression that is not parenthesized, but possibly immediately invoked, and you have real trouble.
The fact that they are useless grammatical relics is the reason why they were gotten rid of.
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