1. 程式人生 > >關於 " +new Date "

關於 " +new Date "

今天 valueof 實例 prototype 字符串 date 進行 class 一個

關於 " +new Date " 的個人見解 今天晚上,在一個Javascript的Q群裏,有人問下面這種代碼是什麽意思: var time = +new Date; 這段代碼中,比較奇怪的是有一個加號,下面說說我個人的理解:這是對後面的對象做一個+運算,觸發對象執行valueOf進行求值! 而Date實例的valueOf返回它的毫秒數,大家可以嘗試如下代碼: var now = new Date, time = now.getTime(); console.log(time==now.valueOf());//true console.log(time==+now);//true 再舉個例子: function Person(){ } Person.prototype.valueOf=function(){ return "hello"; }; var foo = new Person(); console.log("a"+foo); //這裏將輸出字符串"ahello"

關於 " +new Date "