再說js隱式轉換

再說js隱式轉換

本身整理的一個總體規則以下:code

Date 默認 走 toString, 若是 toString 返回的是對象, 那麼查看 valueOf
其餘對象的轉換, 默認走 valueOf, 可是若是 valueOf 返回的是對象, 那麼嘗試 toString對象


//好比示例以下:
//默認的對象隱式轉換是走 valueOf
   var o1 = {
       valueOf: function(){
           return 1;
       },
       toString: function(){
           return 9;
       }
   }

   console.log(o1 == 1); // true


//Date 默認走 toString
 //好比 new Date("1970/01/02")

new Date("1970/01/02").toString() == "Fri Jan 02 1970 00:00:00 GMT+0800 (中國標準時間)"; //true, 默認的 toString 的內容

new Date("1970/01/02") == "Fri Jan 02 1970 00:00:00 GMT+0800 (中國標準時間)"; //true, 默認的 toString 的內容

new Date("1970/01/02").valueOf() == 57600000 // getTime的內容
相關文章
相關標籤/搜索