Here’s a quote from chapter 4 (page 48) of The Book javascript you Don’t Know: Converting non-strings to strings

“For ordinary objects, unless its own definition, otherwise the toString () (Object. The prototype. The toString () returns the value of [[Class]] internal attributes, such as [Object Object]; But if the object has its own toString() method, that method is called when stringed and its return value is returned.”

The following is their own opinion, if there is a wrong place, please big god correct

let f ={a:123}
console.log(f+'a');  // [object object]a

let a ={a:123}
Object.prototype.toString=function(){
  return '555'
}
console.log(a.toString()); / / 555
console.log(a+'a');  // 555a

let e = {
    a:123.toString:function(){
      return '999'}}console.log( e +'a');  // 999a

let g = [1.3.4]
console.log(g+'a');  / / 1 and 4 a
 
let b = [1.3.4]
Array.prototype.toString=function(){
  return '666'
}
console.log(b.toString()); / / 666
console.log(b+'a');  // 666a

let c = 5
Number.prototype.toString=function(){
  return '777'
}
console.log(c.toString()); / / 777
console.log(c+'a');  //5a

let d = '5'
String.prototype.toString=function(){
  return '888'
}
console.log(d.toString()); / / 888
console.log(d+'a');  //5a
Copy the code

Just my personal opinion, just for reference.

Conclusion:
(1) When an object is converted into a string, it first looks for the toString method in itself. If it does not have one, it looks for the toString method in the prototype of its encapsulated object and gets the return value
(2) The Array prototype redefines the toString method to concatenate all unit strings with “,”
(3) When a Number is converted to a string, it can be directly converted to a string. Even if there is a toString method in his Number object prototype, he will not call it. Instead, he will directly convert it.
(4) For the third point, it can also be understood that values of primitive types can be converted directly to strings, so they do not need to call the toString method of their respective archetypes.

The Number() method cast to Number is added here

(1) If the value is a basic type, it will be directly converted
(2) If the value is not a primitive type, first call the prototype valueOf method. If valueOf fails to convert it to a primitive type value, then call the prototype toString method to try to convert it to a primitive type value. If the value can be converted to a primitive type, it is directly converted to a number. If neither valueOf nor toString converts it to a primitive value. Then an error will be reported.
(3) Some special values are converted to Number(true to 1, false to 0, undefined to NaN,null to 0)
Added: parseInt and Number methods
let a ={
  num:12,
  valueOf(){
    return this.num*2
  },
  toString(){
    return this.num*3}}console.log(parseInt(a));  / / 36
console.log(Number(a));  / / 24
Copy the code
ParseInt takes a string value, and even if it’s not a string, it casts it to a string. The above code is passing an object, and the object has to be converted to a string first, so we call toString to get string 36, and parseInt converts string 36.
Number(a) = 24 cause, refer to the Number method above