function now(){
return +new Date;
}
questions :
- what does the plus sign mean?
- when can you create a new object with a constructor function but without the following parentheses, such as
new Date
but notnew Date()
great thanks!
function now(){
return +new Date;
}
questions :
new Date
but not new Date()
great thanks!
1 . The plus sign is the unary + operator.
That expression is equivalent to cast the Date object to number:
function now(){
return Number(new Date);
}
2 . If you don't add the parenthesis, the new operator will call the object type (Date) parameterlessly
Using the plus sign will convert the date into a number (the number of milliseconds since 1 Jan 1970)
You can do this whenever there are no parameters - although you may wish to include them still for readability.