What is Prologue Directives?

丶灬走出姿态 提交于 2019-12-05 09:31:14

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
})();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!