Answer the question
In order to leave comments, you need to log in
How are objects converted in the toString method?
How does converting an object to a string work in this case? And, more importantly... What does a "default" method do here toString
(by "default" I mean a method that has not been manually overridden in the object itself) ?
let a = {
valueOf: function () {
return {};
},
toString: function() {
//conversion to string
}
};
console.log(String(a)); //[object Object]
toString
. Because it cannot return an object and only String(a)
convert that object to a string type. Because then the method toString
will return an object, which is not very good. a method returning a non-primitive data type will be ignored. valueOf
, I think that in all those cases where its behavior has not been overridden, the object itself will be returned. And this method, as a consequence, will almost always be ignored. Simply put, I want to say that the "default" method valueOf
always returns objects.toString
? If so, does the method always toString
convert objects to a string type?valueOf
return objects?Answer the question
In order to leave comments, you need to log in
There are answers to your questions here
https://learn.javascript.ru/object-conversion
https://learn.javascript.ru/prototype
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question