What is Prologue Directives?
Asked Answered
S

1

8

I stumbled upon something people choose to call Prologue Directives. More commonly known with the "use strict"; string literal in JavaScript. Which i already know all about. But the common denominator Prologue Directive. What it is? There's very little documentation available on this subject. Best one is the question i linked.

ECMAScript multiple Prologue Directives

My questions are generic:

What are they?

What can they be used for?

Who uses them and why?

Can i make them?

Should i?

Selfdefense answered 2/3, 2015 at 4:47 Comment(1)
I don't really know enough about prologue directives in general to answer, but here's some more info on Strict mode and why you should use it if you can. Strict mode is the only one that I know of that is widely supported. As the question you linked states, ASM.js is supported/getting support in a few browsers.Deviltry
S
5

No need for documentation. Just look in the source.

A Directive Prologue is the longest sequence of ExpressionStatement productions occurring as the initial SourceElement productions of a Program or FunctionBody and where each ExpressionStatement in the sequence consists entirely of a StringLiteral token followed a semicolon. The semicolon may appear explicitly or may be inserted by automatic semicolon insertion. A Directive Prologue may be an empty sequence.

A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either the exact character sequences "use strict" or 'use strict'. A Use Strict Directive may not contain an EscapeSequence or LineContinuation.

A Directive Prologue may contain more than one Use Strict Directive. However, an implementation may issue a warning if this occurs.

In other words, Directive Prologue is the longest sequence of string literal + semicolon at the exact start of function or program (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 */

Notice that as soon as the string literal is not the first statement, it's no longer a Directive Prologue:

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

or:

(function() {
  1 + "use magic"; // <-- NOT a Directive Prologue
})();
Solis answered 6/3, 2015 at 0:2 Comment(2)
What are they used for aside from "use strict"; ?Selfdefense
For other "non-standard" directives like "use asm", "use 6to5", etc. twitter.com/jashkenas/status/562996829777059840Solis

© 2022 - 2024 — McMap. All rights reserved.