views:

133

answers:

2
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!

+7  A: 

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

CMS
... 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
+2  A: 
  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.

Sohnee