getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
178.28k stars 33.42k forks source link

Spread syntax (...) is not an "operator" #1669

Closed Pointy closed 4 years ago

Pointy commented 4 years ago

I searched for this issue.

2nd Edition,

Get Started (I think)

Chapter: 3

Section Title: Consuming Iterators

The chapter uses the term "operator" in reference to spread syntax (...). It's very common to see that (eg on Stack Overflow) but it's incorrect. An operator is a token in the expression grammar, and spread syntax is not part of the expression grammar in any direct sense. It's involved (for example in array and object initializers) of course, but it isn't like + or >=. It has no evaluation sematics; it does things but it does not itself have a result that could be called a "value" the way that operators do.

This is an excellent and influential book series, so I think it's good to be precise about terminology.

getify commented 4 years ago

I've argued this with folks a number of times (on twitter), and the result is, I'm still convinced I'm right. I think it's an opinion of perception more than a detail about how something is processed by the parser.

https://twitter.com/getify/status/1101852930469183488

Pointy commented 4 years ago

Well, it factually is a detail about how the language is parsed, but it's your book friend-o.

getify commented 4 years ago

I'm not arguing the facts. I am well aware of what the spec says. I've read more of the spec than most.

My point is, there's no difference in approach that a regular user of JS has that needs to differ in any way if they mentally model it as an operator vs as some punctuation-based syntactic feature.

It unquestionably looks like an operator, and the vast majority of JS developers are going to assume it is an operator. That's the perception I'm talking about. Pedantry is the only reason to try to correct all of them.


I often do argue that what the spec explains or calls something matters... but that's only when a case can be made that an inaccurate mental model can cause them further problems. In this case, I don't think there is such a reason. So the overriding concern is to explain things in the most intuitive way possible, which I think is overwhelming that it's assumed to be an operator.

Moreover, this is a book aimed at beginners... the audience for a book matters. If I was writing for advanced developers who implement parsers or JS engines, I of course would sway more toward accuracy.

Pointy commented 4 years ago

Sure, I meant it when I said "it's your book" :) I personally disagree, as I find that an understanding of how a language is actually parsed and interpreted to be valuable, but it's clearly a matter of opinion, and you're definitely right that lots of developers use the term "operator" for ....

getify commented 4 years ago

OK, I'm happy for us to peacefully disagree. Just wanted to make sure you understood my position. Thanks for the feedback. :)