Every object can be converted to a textual representation of it. This is done with the function toString()
. All types have this, without exception.
You may be thinking, but a guy String
, is already a string, then there is no need to convert. It may be, but the function is present to deliver the textual representation of the object. Even because the way this string is organized internally in the object is not your problem, is object exclusive problem, is implementation detail, only function toString()
ensures that it will deliver a textual representation of something, only it allows you to display a text of an object, including on object of type String
.
And you can imagine that in many situations this function toString()
is implicitly called to get the desired result, for example in console.log()
that its function is to print the textual representation of an object. This function does not print implementation details of any object, only the textual representation, even a number, is not printing the number only the text that represents that number. There are other situations that this function is called, and in Javascript this is quite confusing, but it does not come to the case here.
Whenever picking up the value of the object where a text is expected the function will be called. reverse()
expects a text, so in practice what’s happening there in the code is roughly:
console.log('Escola Cod3r'.toString().reverse());
The same way I’d have to do this:
console.log(123..toString().reverse());
Note that JS tends to want to turn a lot of the objects into text, this can be confusing.
And it is the result that is obtained. There is nothing extraordinary, this is the right to do for the sake of keeping everything linear, IE, why treat the type String
how special? If everything needs to be so, so be it.
We have another fan of no-for-a-period who will suffer from the JS confusions.
Ah, Javascript is full of surprises! But it is for this and others that it is not recommended to change the
prototype
of a native type in Javascript.– Wallace Maxters
I gave +1. I find the doubt valid.
– Wallace Maxters
I did some tests. Apparently, this problem only occurs with new methods added to
String.prototype
. I don’t know why, but within the scope of the method created viaprototype
, is considered thetoString
modified instead of original value.– Wallace Maxters
I will relate What is Prototype Pollution and How prototypes work in Javascript
– Wallace Maxters
@Wallacemaxters, I’m on your treadmill. But not to the point of giving up (just taking the down) this overwriting of methods in the chain of prototypes of native objects gives me chills.
– Augusto Vasques
I understand that this overwriting is not something that should be done, because it can affect the code in unpredictable ways, as it happened. But I see no problem exploring the language and increasing knowledge about how it operates.
– vncsbraga
If you’re curious, I don’t have a problem. As we receive questions from users with varying level of knowledge it is difficult to know if the user is only playing with the language or if he takes the concept for granted. If that’s all you take my +1.
– Augusto Vasques
Related also: What is and how the context (this) works in Javascript?
– Luiz Felipe