问题描述
我偶然发现了人们选择称之为Prologue Directives的东西。更常见的是使用JavaScript中的use strict; 字符串文字。我已经知道了。但共同点是Prologue Directive。这是什么?关于这个主题的文献很少。最好的一个是我链接的问题。
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.
我的问题是通用的:
它们是什么?
它们可以用于什么?
谁使用它们以及为什么?
Who uses them and why?
我可以制作吗?
我应该吗?
推荐答案
无需文档。只需查看。
使用严格指令是指令
Prologue中的ExpressionStatement,其StringLiteral是确切的字符序列
use strict或use strict。使用严格指令可能不包含
EscapeSequence或LineContinuation。
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
})()
或:
(function() {
// Directive Prologue start
"foo bar"
"baz";
'123';
'';
// Directive Prologue end
})();
或:
'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
或:
(function() {
1 + "use magic"; // <-- NOT a Directive Prologue
})();
这篇关于什么是序言指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!