You-Dont-Know-JS
You-Dont-Know-JS copied to clipboard
Types & Grammar - Chapter 2 : TypeError while reversing a string.
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.
I think the difference is you're running the code in strict mode.
Nope. It throws the error regardless of strict mode.
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(..)
.
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.
That's what I was trying to say. It's just the commented lines that need to be changed. :)