nzakas / understandinges6

Content for the ebook "Understanding ECMAScript 6"
5.45k stars 796 forks source link

Puzzling code in `Symbol.replace` examples #373

Closed tdd closed 7 years ago

tdd commented 7 years ago

Hi Nicholas,

Location: Chapter 6 > Page 110 > "The …Symbol.replace… properties" > code example

There are two things that puzzle me here, so perhaps I failed to grok something.

First, your Symbol.replace implementation has the following code:

return value.length === 10 ? replacement + value.substring(10) : value

Aside from my personal preference for slice over substring :wink, I fail to see the use of doing a substring(10) over a value guaranteed to be 10 code units long. You'll always get an empty string. So what's the point? Guaranteeing a String due to implicit + coercion semantics? (in which case, just String(replacement) would be nicer). I'm at a loss here.

Second, your demo calling code only provides a single argument to replace, so there should be no result, or undefined, in the replace1 variable. But more generally, such calls don't make much sense usage-wise?

nzakas commented 7 years ago

For the second problem, already reported and fixed: https://github.com/nzakas/understandinges6/issues/349

nzakas commented 7 years ago

For the first issue - I suspect I changed around this example several times while writing and that the substring call is a vestigial artifact from that. I'll clean it up.