在Javascript中,为什么Date对象同时具有valueOf和getTime方法?

时间:2022-10-30 07:31:52

MDN says that valueOf and getTime are functionally equivalent. Why have two functions that do the very same thing?

MDN说valueOf和getTime在功能上是等价的。为什么有两个功能完全相同?

3 个解决方案

#1


37  

The Date.prototype.getTime method returns the number of milliseconds since the epoch (1970-01-01T00:00:00Z); it is unique to the Date type and an important method.

Date.prototype.getTime方法返回自纪元以来的毫秒数(1970-01-01T00:00:00Z);它是Date类型的唯一方法,也是一种重要的方法。

The Object.prototype.valueOf method is used to get the "primitive value" of any object. For the Date class, it is convenient to use the "time" attribute (the value returned by getTime()) as its primitive form since it is a common representation for dates. Moreover, it lets you use arithmetic operators on date objects so you can compare them simply by using comparison operators (<, <=, >, etc).

Object.prototype.valueOf方法用于获取任何对象的“原始值”。对于Date类,使用“time”属性(getTime()返回的值)作为其原始形式是很方便的,因为它是日期的通用表示。此外,它允许您在日期对象上使用算术运算符,因此您可以通过使用比较运算符(<,<=,>等)来比较它们。

var d = new Date();
d.getTime(); // => 1331759119227
d.valueOf(); // => 1331759119227
+d; // => 1331759119227 (implicitly calls "valueOf")
var d2 = new Date();
(d < d2); // => true (d came before d2)

Note that you could implement the "valueOf" method for your own types to do interesting things:

请注意,您可以为自己的类型实现“valueOf”方法来执行有趣的操作:

function Person(name, age) {this.name=name; this.age=age;}
Person.prototype.valueOf = function() {return this.age; }

var youngster = new Person('Jimmy', 12);
var oldtimer = new Person('Hank', 73);
(youngster < oldtimer); // => true
youngster + oldtimer; // => 85

#2


10  

There are no difference in behaviour between those two functions:

这两个函数之间的行为没有区别:

https://code.google.com/p/v8/codesearch#v8/trunk/src/date.js&q=ValueOf&sq=package:v8&l=361

// ECMA 262 - 15.9.5.8
function DateValueOf() {
  return UTC_DATE_VALUE(this);
}

// ECMA 262 - 15.9.5.9
function DateGetTime() {
  return UTC_DATE_VALUE(this);
}

But there are historical differences.

但是有历史的差异。

#3


4  

valueOf is a method of all objects. Objects are free to override this to be what they want.

valueOf是所有对象的方法。对象可以*地重写它们是他们想要的。

#1


37  

The Date.prototype.getTime method returns the number of milliseconds since the epoch (1970-01-01T00:00:00Z); it is unique to the Date type and an important method.

Date.prototype.getTime方法返回自纪元以来的毫秒数(1970-01-01T00:00:00Z);它是Date类型的唯一方法,也是一种重要的方法。

The Object.prototype.valueOf method is used to get the "primitive value" of any object. For the Date class, it is convenient to use the "time" attribute (the value returned by getTime()) as its primitive form since it is a common representation for dates. Moreover, it lets you use arithmetic operators on date objects so you can compare them simply by using comparison operators (<, <=, >, etc).

Object.prototype.valueOf方法用于获取任何对象的“原始值”。对于Date类,使用“time”属性(getTime()返回的值)作为其原始形式是很方便的,因为它是日期的通用表示。此外,它允许您在日期对象上使用算术运算符,因此您可以通过使用比较运算符(<,<=,>等)来比较它们。

var d = new Date();
d.getTime(); // => 1331759119227
d.valueOf(); // => 1331759119227
+d; // => 1331759119227 (implicitly calls "valueOf")
var d2 = new Date();
(d < d2); // => true (d came before d2)

Note that you could implement the "valueOf" method for your own types to do interesting things:

请注意,您可以为自己的类型实现“valueOf”方法来执行有趣的操作:

function Person(name, age) {this.name=name; this.age=age;}
Person.prototype.valueOf = function() {return this.age; }

var youngster = new Person('Jimmy', 12);
var oldtimer = new Person('Hank', 73);
(youngster < oldtimer); // => true
youngster + oldtimer; // => 85

#2


10  

There are no difference in behaviour between those two functions:

这两个函数之间的行为没有区别:

https://code.google.com/p/v8/codesearch#v8/trunk/src/date.js&q=ValueOf&sq=package:v8&l=361

// ECMA 262 - 15.9.5.8
function DateValueOf() {
  return UTC_DATE_VALUE(this);
}

// ECMA 262 - 15.9.5.9
function DateGetTime() {
  return UTC_DATE_VALUE(this);
}

But there are historical differences.

但是有历史的差异。

#3


4  

valueOf is a method of all objects. Objects are free to override this to be what they want.

valueOf是所有对象的方法。对象可以*地重写它们是他们想要的。