ES6 引入的代码严格吗?
Is ES6 imported code strict?
假设我有一个依赖于非严格语义的库,例如将顶级 this
强制为全局对象:
this.library = { foo: function () {} };
说我尝试使用 import
:
导入这个库
import './library';
library.foo();
因为我使用了 import
,这是否意味着库代码在严格模式下隐式运行(并且中断,因为 this
在严格代码中是 undefined
)? ECMAScript 规范 says,
Module code is always strict mode code.
以及 "module code,"
的 definition
Module code is source text that is code that is provided as a ModuleBody
.
还有一个ModuleBody
consists of a ModuleItemList
, which consists of ModuleItem
's, which could include ImportDeclaration
, ExportDeclaration
and StatementListItem
, and a StatementListItem
could be一个Statement
或Declaration
。这意味着任何代码都可以 "module code" 取决于它加载的上下文,即使没有 import
或 export
声明。
此外,15.2.1.16 中 [[RequestedModules]]
字段的定义意味着 ImportDeclaration
中使用的 ModuleSpecifier
确实指定了 "module:"
A List of all the ModuleSpecifier
strings used by the module represented by this record to request the importation of a module.
模块系统似乎与非严格代码向后不兼容。这是真的吗?
A module is not a JavaScript program that includes import/export
statements. Just because they MAY contain import/export statements
does not mean that they MUST have them. You cannot always look at a
program and determine whether or not it is a module. . . .
Since ModuleBody is optional, the empty program is a Module.
The empty program does not contain an export statement.
所以,是的,模块系统是 backwards-incompatible 和 non-strict 代码。我认为不使用模块系统的代码并不适合参与该系统,因为它可能会导出全局变量,这违背了它的目的。
因此,为了互操作,必须更新旧代码以提供 export
声明或使用严格模式(and/or "guaranteed" non-strict Function('return this')()
之类的技术,而不是 应该 non-strict top-level this
).
假设我有一个依赖于非严格语义的库,例如将顶级 this
强制为全局对象:
this.library = { foo: function () {} };
说我尝试使用 import
:
import './library';
library.foo();
因为我使用了 import
,这是否意味着库代码在严格模式下隐式运行(并且中断,因为 this
在严格代码中是 undefined
)? ECMAScript 规范 says,
Module code is always strict mode code.
以及 "module code,"
的 definitionModule code is source text that is code that is provided as a
ModuleBody
.
还有一个ModuleBody
consists of a ModuleItemList
, which consists of ModuleItem
's, which could include ImportDeclaration
, ExportDeclaration
and StatementListItem
, and a StatementListItem
could be一个Statement
或Declaration
。这意味着任何代码都可以 "module code" 取决于它加载的上下文,即使没有 import
或 export
声明。
此外,15.2.1.16 中 [[RequestedModules]]
字段的定义意味着 ImportDeclaration
中使用的 ModuleSpecifier
确实指定了 "module:"
A List of all the
ModuleSpecifier
strings used by the module represented by this record to request the importation of a module.
模块系统似乎与非严格代码向后不兼容。这是真的吗?
A module is not a JavaScript program that includes import/export statements. Just because they MAY contain import/export statements does not mean that they MUST have them. You cannot always look at a program and determine whether or not it is a module. . . .
Since ModuleBody is optional, the empty program is a Module. The empty program does not contain an export statement.
所以,是的,模块系统是 backwards-incompatible 和 non-strict 代码。我认为不使用模块系统的代码并不适合参与该系统,因为它可能会导出全局变量,这违背了它的目的。
因此,为了互操作,必须更新旧代码以提供 export
声明或使用严格模式(and/or "guaranteed" non-strict Function('return this')()
之类的技术,而不是 应该 non-strict top-level this
).