Question: Question:
Will what happens only when "use strict" is set in ECMAScript5 continue in ECMAScript6?
-For example, when a function is called, will "the phenomenon that this becomes undefined in strict mode and becomes global this (window in the browser) in non-strict mode" continue?
-If you do not explicitly add "use strict" as usual, will it not be undefined in the non-strict mode?
Answer: Answer:
The Strict Mode distinction continues in ECMAScript 2015 (ES6) / ECMAScript 2016.
Existing behavior cannot be modified for backwards compatibility.
However, where there is no need to consider backward compatibility, it defaults to Strict Mode behavior.
In particular,
- Inside the Class definition
- Whole code loaded as Modules
is.
Inside the Class definition
class A {
foo() {
console.log("ここは Strict Mode です");
}
}
console.log("ここは Strict Mode ではありません");
function foo() {
"use strict";
console.log("ここは Strict Mode です");
}
Whole code loaded as Modules
In the browser, if you load the JavaScript code as type="module"
as follows, the code will be treated as a module.
<script type="module" src="foo.js"></script>
All code loaded as a module will be forced into Strict Mode.
See Also: See Also:
- http://www.ecma-international.org/ecma-262/6.0/#sec-strict-mode-code
- https://html.spec.whatwg.org/multipage/scripting.html#attr-script-type