ECMAScript multiple Prologue Directives
Asked Answered
S

2

5

Certain ECMAScript environments permit switiching into a special mode by means of a Directive Prologue. ECMAScript 5 has "use strict" and others such as asm have their own like "use asm".

The docs on Directive Prologues are written in a language that's a little to obtuse for my comprehension level. What is the correct way to construct a Directive Prologue with multiple Directives? My hunch is its:

function(){
  "use foo";
  "use bar";
}

But I'm not sure.

Sarpedon answered 15/2, 2013 at 20:2 Comment(3)
Your hunch is correct. My hunch is that no other directives than "use strict" are actually recognised by anyone, at least not in the browser environment.Swamp
Yes, that's correct. String literal followed by a semicolon (semicolon could be inserted automatically).Chabot
Firefox 22 should recognize "use asm";Omora
C
3

What is the correct way to construct a Directive Prologue with multiple Directives?

As the spec you linked says,

a Directive Prologue is the longest sequence of ExpressionStatement productions occurring [at the begin of a script or function] and where each [of them] consists entirely of a StringLiteral.

So you can just string them together, every of these string-literal-statements is a Directive; and can have an implementation-specific meaning (only the Use-Strict-Directive is specified). Your hunch is correct, this should work:

"use bar"
"use strict"; 'use x';
'use foo';
Choong answered 19/2, 2013 at 23:38 Comment(0)
S
1

Since no one answered it, but I found the answer and it was confirmed in a comment I'm answering my own to close it.

Yes, to use multiple directives in a prologue list them one after the other like so:

function(){
  "use foo";
  "use bar";
}

or

function(){
  "use foo"; "use bar";
}
Sarpedon answered 19/2, 2013 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.