how does the built in date class work in javascript?
for instance:
var timestamp = new Date('02/14/1994')
// returns milliseconds without having to call timestamp.miliseconds
// it just passes it into the variable
Although your question is not exactly clear on this I suspect you came across something 2 special methods with JavaScript classes that are automatically being called when an object is converted to a string or is compared with another value:
Given the following class we can play around with these:
class Test { toString() { return 'Hello world'; } valueOf() { return 3; } } const t = new Test(); console.log( t ); console.log( t.toString() ); console.log( `${ t }` ); console.log( t > 2 );
The Date
object is very similar here. When you call (new Date()).toString()
you will get the full date printed out. If you do cast it into a number with +(new Date())
the internal valueOf()
is being used to convert it into its numeric value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.