What are the prologue directives?

I came across what people prefer to call Prologue Directives. It is well known that "the use of strict"; JavaScript string literal. Which I already know everything. But the general pointer is Prologue Directive. What is it? There is very little documentation on this subject. The best one is the question I have related.

Several ECMAScript Prologue Directives

My questions are general:

What are they?

What can they be used for?

Who uses them and why?

Can i make them?

Should I?

+4
source share
1 answer

No need for documentation. Just look at the source .

The Prologue directive is the longest sequence of ExpressionStatement productions occurring as the original output of the SourceElement Program or FunctionBody and where each ExpressionStatement in the sequence consists solely of the StringLiteral token following the semicolon. The semicolon may be displayed explicitly or an automatic semicolon may be inserted. The Prolog directive may be an empty sequence.

A Use of a strict directive is an ExpressionStatement expression in the Prolog directive whose StringLiteral is either the exact sequence of characters "use strict" or "use strict". Using a strict directive may not contain EscapeSequence or LineContinuation.

The Prologue directive may contain several rules for using Strict. However, an implementation may issue a warning if this happens.

In other words, Directive Prologue is the longest string literal sequence + semicolon when a function or program starts up accurately (top-level code):

(function(){ "use strict"; // <-- Directive Prologue })() 

or

 (function() { // Directive Prologue start "foo bar" "baz"; '123'; ''; // Directive Prologue end })(); 

or

 'blah'; // <-- Directive Prologue (top-level code) /* rest of the code here */ 

Note that once a string literal is not the first statement, it is no longer a directory prolog:

 var x; "use strict"; // <-- NOT a Directive Prologue 

or

 (function() { 1 + "use magic"; // <-- NOT a Directive Prologue })(); 
+1
source

All Articles