Skip to content

Commit 91869c9

Browse files
committed
Update Appendix B with strict directive restriction
1 parent 0da344a commit 91869c9

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

manuscript/B-ECMAScript-7.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The development of ECMAScript 6 took about four years, and after that, TC-39 dec
44

55
More frequent releases mean that each new edition of ECMAScript should have fewer new features than ECMAScript 6. To signify this change, new versions of the specification no longer prominently feature the edition number, and instead refer to the year in which the specification was published. As a result, ECMAScript 6 is also known as ECMAScript 2015, and ECMAScript 7 is formally known as ECMAScript 2016. TC-39 expects to use the year-based naming system for all future ECMAScript editions.
66

7-
ECMAScript 2016 was finalized in March 2016 and contained only two additions to the language: a new mathematical operator and a new array method. Both are covered in this appendix.
7+
ECMAScript 2016 was finalized in March 2016 and contained only three additions to the language: a new mathematical operator, a new array method, and a new syntax error. Both are covered in this appendix.
88

99
## The Exponentiation Operator
1010

@@ -112,3 +112,46 @@ console.log(values.includes(-0)); // true
112112
```
113113

114114
Here, both `indexOf()` and `includes()` find `+0` when `-0` is passed because the two values are considered equal. Note that this is different than the behavior of the `Object.is()` method, which considers `+0` and `-0` to be different values.
115+
116+
## Change to Function-Scoped Strict Mode
117+
118+
When strict mode was introduced in ECMAScript 5, the language was quite a bit simpler than it became in ECMAScript 6. Despite that, ECMAScript 6 still allowed you to specify strict mode using the `"use strict"` directive either in the global scope (which would make all code run in strict mode) or in a function scope (so only the function would run in strict mode). The latter ended up being a problem in ECMAScript 6 due to the more complex ways that parameters could be defined, specifically, with destructuring and default parameter values. To understand the problem, consider the following code:
119+
120+
```js
121+
function doSomething(first = this) {
122+
"use strict";
123+
124+
return first;
125+
}
126+
```
127+
128+
Here, the named parameter `first` is assigned a default value of `this`. What would you expect the value of `first` to be? The ECMAScript 6 specification instructed JavaScript engines to treat the parameters as being run in strict mode in this case, so `this` should be equal to `undefined`. However, implementing parameters running in strict mode when `"use strict"` is present inside the function turned out to be quite difficult because parameter default values can be functions as well. This difficulty led to most JavaScript engines not implementing this feature (so `this` would be equal to the global object).
129+
130+
As a result of the implementation difficulty, ECMAScript 2016 makes it illegal to have a `"use strict"` directive inside of a function whose parameters are either destructured or have default values. Only *simple parameter lists*, those that don't contain destructuring or default values, are allowed when `"use strict"` is present in the body of a function. Here are some examples:
131+
132+
```js
133+
// okay - using simple parameter list
134+
function okay(first, second) {
135+
"use strict";
136+
137+
return first;
138+
}
139+
140+
// syntax error
141+
function notOkay1(first, second=first) {
142+
"use strict";
143+
144+
return first;
145+
}
146+
147+
// syntax error
148+
function notOkay2({ first, second }) {
149+
"use strict";
150+
151+
return first;
152+
}
153+
```
154+
155+
You can still use `"use strict"` with simple parameter lists, which is why `okay()` works as you would expect (the same as it would in ECMAScript 5). The `notOkay1()` function is a syntax error because you can no longer use `"use strict"` in functions with default parameter values. Similarly, the `notOkay2()` function is a syntax error because you can't use `"use strict" in a function with destructured parameters.
156+
157+
Overall, this change removes both a point of confusion for JavaScript developers and an implementation problem for JavaScript engines.

0 commit comments

Comments
 (0)