Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
animeshk874 opened this issue May 3, 2018 · 5 comments
Open

Comments

@animeshk874
Copy link

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

@getify
Copy link
Owner

getify commented May 3, 2018

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

@animeshk874
Copy link
Author

Nope. It throws the error regardless of strict mode.

@getify
Copy link
Owner

getify commented May 3, 2018

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
Copy link
Owner

getify commented May 3, 2018

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.

@animeshk874
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants