Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the purpose of Labeled function declarations

Tags:

javascript

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.

like image 571
Max Koretskyi Avatar asked Oct 27 '16 06:10

Max Koretskyi


People also ask

What is the point of a function declaration?

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.

Why function declaration is placed prior to function definition?

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.

What is function declaration in JavaScript?

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.

What components of a function declaration must be included?

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.

When should I use function declarations and expressions?

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.

Are labeled function declarations in ECMAScript 2015 standardized?

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 :

Which is an example of the JavaScript declaration?

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.

What are the functions of labeling?

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?


1 Answers

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.

like image 156
Amadan Avatar answered Oct 27 '22 23:10

Amadan