getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question about "if a file has strict mode turned on, the function-level strict mode pragmas are disallowed"

SiarheiBobryk opened this issue · comments

"I already searched for this issue"

Edition: 2nd

Book Title: You Don't Know JS Yet: Get Started

Chapter: 1

Section Title: Strictly Speaking

Question:

According to the "Strictly Speaking" section

Interestingly, if a file has strict mode turned on, the function-level strict mode pragmas are disallowed. So you have to pick one or the other.

Let's say I'd like to violate this. What is the expected behaviour for that case?

P.S. I've tried to play with some examples, but I cannot see any differences.

I am trying to get a more concrete answer to your question including what I was specifically indicating. But here's one example of what I had in mind:

3

Yeah, so that example is what was buried deep in the back of my mind.

The book's text is a tad misleading in its brevity and not describing the full context. It's not that you can't have duplicate pragmas anywhere.

What's really at issue is that a function that has non-simple parameters (e.g., a = 1, ...rest, or [ a, { b } ]) cannot have a strict-mode pragma in it at all (regardless of the file level pragma being present or not). There's a nuanced reason why TC39 chose to disallow it: the non-simple parameters actually are evaluated in an intermediate scope that's technically outside the function body's scope, so a function body scope's own pragma cannot rationally affect the behavior of these parameters, as one might typically expect just glancing at code. TC39 didn't want devs to assume the function-level strict-mode pragma was affecting the parameters, so they chose to syntactically disallow it to avoid the confusion case.

I had long-ago argued with some members of TC39 that it should be allowed in the specific case where there pragma is also at the top of the file, since the file-level pragma ensures the parameters are evaluated in strict-mode anyway, so the function body pragma was a no-op and should thus be passively allowed. They didn't agree.

So... long story short, only part of that detailed issue was bubbling in my head when I wrote the text in question into the book.

In retrospect, I'm not sure if I should have left out the comment, or if I should have elaborated to explain the full context.

Oh well.

Thanks for the quick reply @getify
It's much clear for me now and I got the idea 👍

Maybe it might be worth adding a quick note about that in the book?

P.S. Thanks again for your books @getify cause you make us stronger and wiser ❤️