Should `use strict` be a valid strict pragma?
No it should not. "use string" and 'use strict' are specially recognized syntax. The only reason that they reuse the syntax of expression statements, rather than being, for example,
use strict;
without the quotes, is so this pragma would be ignored on older pre-ES5 browsers without causing the programs to break. It is not in any meaningful way string-like.
`use strict`
should no more be recognized that should ("use " + "strict")
.
Well, that isn't quite the full story - if it were just a case of pragmas
having to use something, anything, that could pass ES3 engines, then there
wouldn't necessarily be two otherwise-redundant forms of the syntax - "use strict"
and 'use strict'
. The reason those exist is to save the author
remembering which string delimiter to use - it mirrors the string literal
syntax exactly. One of the "For" arguments I gave purports that the
backtick is also a delimiter for what are in essence string literals,
albeit literals with enhanced, never-before-seen capabilities - and that by
not permitting `use strict`
as a strict pragma, the aforementioned
visual symmetry between string literals and pragmas is broken.
(It is of course easily understood that "use" + " strict"
is not a pragma
for the obvious reason that it's syntactically two literals joined by an
operator. `use strict`
, on the other hand, is just as much a single
literal as "use strict"
is.)
On 5 Feb 2015, at 11:04, Leon Arnott <leonarnott at gmail.com> wrote:
Well, that isn't quite the full story - if it were just a case of pragmas having to use something, anything, that could pass ES3 engines, then there wouldn't necessarily be two otherwise-redundant forms of the syntax -
"use strict"
and'use strict'
. The reason those exist is to save the author remembering which string delimiter to use - it mirrors the string literal syntax exactly.
If that were the case, then e.g. '\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74'
would trigger strict mode. (It doesn’t, and that’s a good thing.)
I think you're missing the point Leon is trying to make. He's saying that,
in ES 6 we have a new way to write strings. In some ways, these more
powerful strings may condition some people to use `
as their main string
delimiter. An unsuspecting person may liken this to PHP's double quotes vs
single quotes, thinking that the only difference is that you can use
`${variable}`
in strings that are delimited with backticks, but other than
that everything is the same. When they write this in their code:
`use strict`;
They may introduce bugs by writing non-strict code that doesn't throw when it should. Adding it to the spec wouldn't be difficult and it would avoid any potential confusion or difficult-to-debug issues. It's definitely easier than educating people, IMO.
I think any issues with that are imagined. Languages have rules, and of the people who both know what 'use strict' does and are using es6 syntax, they're very unlikely to make the mistake.
I don't see people using template literals for arbitrary strings... it could happen but it probably won't. Mathias makes a good point also, it's not strings that equal the string 'use strict', it's exactly two possible arrangements of characters.
An HTML attachment was scrubbed... URL: esdiscuss/attachments/20150205/a94b92fa/attachment
On Feb 5, 2015, at 7:24 AM, Alex Kocharin wrote:
Why is there two of them, not one?
It was my invention, so take this as definitive...
In 2008 (or maybe 7) when we were came up with the strings for mode directives idea, there was no obvious preference among JS programmers in their use of " and ' for string literals. So since there was no reason to prefer one over the other we allowed for both.
On 5 Feb 2015 15:06, "Frankie Bagnardi" <f.bagnardi at gmail.com> wrote:
I think any issues with that are imagined. Languages have rules, and of
the people who both know what 'use strict' does and are using es6 syntax, they're very unlikely to make the mistake.
Sure, it's theoretical at this point but not unimaginable. Eventually everyone will be using "es6 syntax", and there are plenty of blogs and books around that explain the benefits of strict mode.
I don't see people using template literals for arbitrary strings... it
could happen but it probably won't.
Not everyone, but I think it's likely that some will. If you want to add a variable ref to a static string, for instance, it's simpler if you don't have to change the delimiters to back ticks. We can probably only speculate right now.
Mathias makes a good point also, it's not strings that equal the string 'use strict', it's exactly two possible arrangements of characters.
And that's obvious to us, but won't be to everyone. I'm on my phone right now so it's hard to check, but I think you'll find more sources explaining the directive is a string literal than you would an arrangement of characters.
My immediate reaction was that it wasn't worth it, but I kinda think that if it is easy enough to implement then any risk would be avoided by doing so.
'use strict' and "use strict" are magic tokens and should stay that way, not propagate to other ways of writing literal strings. Literal strings are different things, which happen to share the same syntax for backwards-compatibility reasons.
If people switch to backticks for all their literal strings, so much the better -- then single and double quotes will only be used for directives, and there will be less confusion. (I don't actually believe that. At the very least, I don't expect JSON to allow backticks anytime soon. Nor do I think that using backticks indiscriminately is good practice.)
Also: given that modules are implicitly strict, you will hardly ever use the strict directive in ES6.
Given that
"use strict"
and'use strict'
are both valid strict pragmas, should`use strict`
in the same position also be regarded as a valid strict pragma?Against:
For:
let
as "the new var", spread as "the new.apply
" or arrow syntax as "the newfunction
". Restricting the use of backticks in this common string usage seems unwarranted.Neutral:
Thoughts?