ECMAScript multiple Prologue Directives

£可爱£侵袭症+ 提交于 2019-12-01 07:06:22

问题


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.


回答1:


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';



回答2:


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";
}


来源:https://stackoverflow.com/questions/14902465/ecmascript-multiple-prologue-directives

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