You-Dont-Know-JS icon indicating copy to clipboard operation
You-Dont-Know-JS copied to clipboard

Types & Grammar - Chapter 2 : TypeError while reversing a string.

Open animeshk874 opened this issue 6 years ago • 5 comments

https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch2.md#strings There's a code in this section that tries to use the Array.prototype.reverse function to reverse a string.

Array.prototype.reverse.call( a );
// still returns a String object wrapper (see Chapter 3)
// for "foo" :(

The comment says that it returns a String object wrapper. However, running it on Chrome 66 (and maybe other browsers too) gives a TypeError.

screenshot from 2018-05-03 18-21-27

animeshk874 avatar May 03 '18 12:05 animeshk874

I think the difference is you're running the code in strict mode.

getify avatar May 03 '18 14:05 getify

Nope. It throws the error regardless of strict mode.

animeshk874 avatar May 03 '18 14:05 animeshk874

Shrugs. This must have changed at some point. It used to silently ignore the attempt to assign to the read-only string when I wrote the book. I think what may have changed is the treatment of values that are passed through as this via .call(..).

getify avatar May 03 '18 18:05 getify

Note that the section in question is accurate in that it says Array.prototype.reverse(..) does not work. The manner in which it does not work is what has changed.

getify avatar May 03 '18 18:05 getify

That's what I was trying to say. It's just the commented lines that need to be changed. :)

animeshk874 avatar May 03 '18 18:05 animeshk874