Live data from Hacker News

How to Control JavaScript's Strict Mode

jskatas.org

11–17 of 17 posts

Re: How to Control JavaScript's Strict Mode

#11
post #8

Earlier quoted context omitted.

It’s about strictness scoping. https://old.reddit.com/r/webdev/comments/v877zq/why_cant_you... gives a decent explanation, and https://github.com/rwaldron/tc39-notes/blob/d0c651b358b361b0... is good and technical.

That seems weird to me. Does strict mode affect parsing? I thought that parsing completed before execution, so why couldn’t the parser generate the same AST for these two functions? (a) => a === undefined ? 1 : a (a = 1) => a

> Does strict mode affect parsing?

Actually, yes. default parameter values could contain things that are allowed in sloppy mode and are syntax errors in strict mode. For instance, a leading zero in numbers without explicitly specifying the base, duplicate property names, the with statement, new reserved words and using delete on a variable name.

You need to disallow those in the parameter values in strict mode, but you only know this after encountering the "use strict" marking in the function body if you were not already in strict mode. You can make a parser able to do this, but it is more complex. I guess they decided that this complexity is not worth it.

Re: How to Control JavaScript's Strict Mode

#12
post #9

Nobody really likes to talk about that time Perl and Javascript got drunk and hooked up.

It happened at least twice.

The first time, it gave jQuery, which notably inherited the $ and the terser syntax from perl. The second time, indeed, JS found Perl's "use strict" quite cute.

Re: How to Control JavaScript's Strict Mode

#13
post #9

Nobody really likes to talk about that time Perl and Javascript got drunk and hooked up.

At least Perl "uses" `use` for more things than just strict mode.

As a beginner, I remember finding this magical literal that (might) alter JS behavior pretty strange, especially since there were no other `use` directives.

I really like Perl's `use diagnostics` directive, which provides more verbose error messages, and am frankly surprised a far more popular language that is supposed to be beginner-friendly does not have something similar.

Re: How to Control JavaScript's Strict Mode

#14
post #12
post #9

Nobody really likes to talk about that time Perl and Javascript got drunk and hooked up.

It happened at least twice. The first time, it gave jQuery, which notably inherited the $ and the terser syntax from perl. The second time, indeed, JS found Perl's "use strict" quite cute.

I disagree that jquery adopted anything from perl. jQuery's $ is not a sigil.

Re: How to Control JavaScript's Strict Mode

#15
post #12
post #9

Nobody really likes to talk about that time Perl and Javascript got drunk and hooked up.

It happened at least twice. The first time, it gave jQuery, which notably inherited the $ and the terser syntax from perl. The second time, indeed, JS found Perl's "use strict" quite cute.

It was originally `use strict` without any quotes, along with other possible `use ...` statements in the scrapped ES4 working draft. It seems that the exact person suggested this particular syntax was David-Sarah Hopwood [1], which suggestion was quickly adopted to the next draft of what eventually became ES5.

[1] https://web.archive.org/web/20150525070848/https://mail.mozi...

Re: How to Control JavaScript's Strict Mode

#16
post #12

Earlier quoted context omitted.

It happened at least twice. The first time, it gave jQuery, which notably inherited the $ and the terser syntax from perl. The second time, indeed, JS found Perl's "use strict" quite cute.

It was originally `use strict` without any quotes, along with other possible `use ...` statements in the scrapped ES4 working draft. It seems that the exact person suggested this particular syntax was David-Sarah Hopwood [1], which suggestion was quickly adopted to the next draft of what eventually became ES5. [1] https://web.archive.org/web/20150525070848/https://mail.mozi...

It was good that it's a string literal, so it did not make the file incompatible with parsers that didn't support strict mode. You'd have to make sure your code behaved correctly both in sloppy mode and in strict mode.

Probably irrelevant today.

Re: How to Control JavaScript's Strict Mode

#17
post #16

Earlier quoted context omitted.

It was originally `use strict` without any quotes, along with other possible `use ...` statements in the scrapped ES4 working draft. It seems that the exact person suggested this particular syntax was David-Sarah Hopwood [1], which suggestion was quickly adopted to the next draft of what eventually became ES5. [1] https://web.archive.org/web/20150525070848/https://mail.mozi...

It was good that it's a string literal, so it did not make the file incompatible with parsers that didn't support strict mode. You'd have to make sure your code behaved correctly both in sloppy mode and in strict mode. Probably irrelevant today.

That indeed was a big concern, and the initial proposal assumed that it at least has to survive the lexical analysis. At some point there were two syntaxes, `use strict` (optionally followed by a comma and other tokens) or `if (false) use(strict)` at one's choice. In comparison Hopwood's proposal was not even an actual proposal but quickly adopted due to its simplicity and robustness. The only complaint was a space in the literal.
Post reply on HN