Date()和new Date()區別


當任意一個普通函數用於創建一類對象時,它就被稱作構造函數,或構造器。

  • new操作符來調用一個構造函數時,創建一個空對象obj,
  • 將這個空對象的__proto__成員指向了構造函數對象的prototype成員對象
  • Date()就是一個靜態方法、普通函數返回一個時間string作為普通函數的返回值
typeof Number(10)//number
typeof new Number(10)//object
typeof Date()//string
typrof new Date()//object
function MyNumber() {
    var args = [].slice.call(arguments);
    //console.log(args);
    this.args = args;
    return args.toString();
}

MyNumber.prototype.toString = function() {
    return this.args.toString();
}
var mm = MyNumber(1,2,3);
console.log(typeof mm);    //string
console.log(mm);           //1,2,3
var o = new MyNumber(1,2,3);
console.log(typeof o);   //object
console.log(o);          //MyNumber {args: Array[3]}
console.log(o.toString()); //1,2,3

https://segmentfault.com/q/1010000004410988

ps

Date.now()返回當前日期的毫秒數,效率更高

new Date()//Fri Jun 09 2017 16:40:08 GMT+0800 (中國標准時間)
Date.now()//1496997617950

if (!Date.now) {
  Date.now = function now() {
    return new Date().getTime();
  };
}

image

2017/6/9


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM