内置日期类如何在javascript中工作?
例如:
var timestamp = new Date('02/14/1994')
// returns milliseconds without having to call timestamp.miliseconds
// it just passes it into the variable
尽管您的问题尚不清楚,但是我怀疑您遇到了2种特殊的JavaScript类方法,当对象转换为字符串或与另一个值进行比较时,这些方法会自动被调用:
鉴于以下课程,我们可以玩这些课程:
class Test {
toString() {
return 'Hello world';
}
valueOf() {
return 3;
}
}
const t = new Test();
console.log( t );
console.log( t > 2 );
Date
对象在这里非常相似。当您拨打(new Date()).toString()
时,您将打印出完整的日期。如果确实使用+(new Date())
将其转换为数字,则将使用内部valueOf()
将其转换为数字值。
这是不正确的,新的Date返回对象,而构造方法总是返回对象