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