6 ms·
I can see that strict mode inside a function using default parameters should throw according to the spec ( https://tc39.github.io/ecma262/#sec-function-definiti
by nailer 10y ago
I can see that strict mode inside a function using default parameters should throw according to the spec ( https://tc39.github.io/ecma262/#sec-function-definitions-static-semantics-early-errors https://tc39.github.io/ecma262/#sec-function-definitions-sta...) but does anyone know why? Is strict mode something we should now be avoiding?
- diggan 10y agoAccording to the documentation on MDN[0], it's a part of "early errors" and you should rather but the "use strict" outside the function instead of on the inside. I'm not entirely sure of why the spec wants it to be outside rather than inside though... - [0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Strict_Non_Simple_Params https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- tomatsu 10y ago> you should rather [put] the "use strict" outside the function instead of on the inside. Modules and classes are always in strict mode. In practice, it really isn't an issue.
- swhipple 10y agoSee here [1], under "Why make this change?". [1] https://www.nczonline.net/blog/2016/10/the-ecmascript-2016-change-you-probably-dont-know/ https://www.nczonline.net/blog/2016/10/the-ecmascript-2016-c...
- nailer 10y agoAh that makes sense - by the time the parser sees a 'use strict' it's too late to apply strict mode to the default arguments. Thanks!
- bushin 10y agoNicholas Zakas has a nice explanation https://leanpub.com/understandinges6/read#leanpub-auto-change-to-function-scoped-strict-mode https://leanpub.com/understandinges6/read#leanpub-auto-chang...
- TheAceOfHearts 10y agoIf you're using a bundler and transpiler, ES modules are already in strict mode.
- nailer 10y agoLike most folk I don't use TC39 modules, just npm/commonjs modules. I guess when/if npm and node (and all the frontend tooling that uses npm and node) starts using TC39 modules they'll get popular.
- z3t4 10y agoEs6 modules suck because they will litter the global scope, while commonjs/nodejs modules can be required locally.
- allover 10y agoES6 modules are great because they're statically analysable, whilst CommonJS modules aren't.
- z3t4 10y agojust parse it like any other js code
- tomatsu 10y agoDeclarations can be analyzed statically. Declarative stuff is convenient that way. If you have to execute code, things get messy.
- allover 10y agoBy that do you mean you can 'just parse' CommonJS modules? (If so) CommonJS allows you to dynamically alter module.exports anywhere within your module. You can't determine what those exports are just by parsing, you'd need to execute the code.
- kozak 10y ago"use strict" has to be outside of the function in this case.
- uwu 10y agomy guess: to not complicate parsing complex expressions in default parameters // non-strict mode top-level code function foo (complex = eval("1+1"), moreComplex = (function(){ with (this) { return bar; } }())) { "use strict"; // go back and parse the parameters again but this time with strict mode semantics // ... }