About the strange features in JS

var a = {
    value:2,
    toString:function()
    {
        console.log("........");
        return PPthis.value;
    }
}

if(a==3 && a==4){
    console.log("JS is amazing");
}

as above, using toString will output "JS is amazing". If I try to replace it, such as toLocaleString, it won"t be output. what feature of JS makes this?

Mar.04,2021

when using = instead of identity, implicit conversion will be triggered if the original value is not equal, that is, an implicit conversion will be triggered by calling the toString method of the left value to convert it into a string, so it cannot be achieved when you replace toString with toLocaleString .


when judging, the toString method will be called if it is repeated in the object. Replace it with toLocaleString and it will find the toString method on the prototype

Menu