4
function now(){
    return +new Date;
}

questions :

  1. what does the plus sign mean?
  2. when can you create a new object with a constructor function but without the following parentheses, such as new Date but not new Date()

great thanks!

user133580
  • 1,479
  • 1
  • 19
  • 28
  • 3
    1. see http://stackoverflow.com/questions/221539/what-does-the-plus-sign-do-in-return-new-date – harto Jul 08 '09 at 07:00

2 Answers2

8

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

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 1
    ... and converting a `Date` into a `Number` (any way you do it) is, basically, just a less readable way of invoking `getTime()`: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/getTime – gustafc Jul 08 '09 at 07:25
3
  1. Using the plus sign will convert the date into a number (the number of milliseconds since 1 Jan 1970)

  2. You can do this whenever there are no parameters - although you may wish to include them still for readability.

Fenton
  • 241,084
  • 71
  • 387
  • 401