JS的toString和valueOf

var a={
    i:1,
    //valueOf:
    toString:function(){
        if(this.i===1){
            this.i++;
            return 1
        }else{
            return 12
        }
    }
}
if(a==1&&a==12){
    console.log(123)
}

valueOf()方法和toString()方法是一样的,都会在后台进行隐式的调用。

原文地址:https://www.cnblogs.com/ckAng/p/12888546.html