views:

226

answers:

1

Hello,

In JavaScript, it seems you can do either write:

new Date().getTime();

...or:

(new Date).getTime();

The first one is logical, but the second one seems a little unusual to me... Is there any difference between these two ways of creating a Date object, and what is the purpose of the second?

Thanks,

Steve

+4  A: 

It seems that in javascript you can call constructor without parethesis. At least it works with my Firefox. So (new Date) == new Date()

Implying from that both expressions are equivalent. Alternatively you could write

(new Date()).getTime();

Which is what I usually do.

And I think it's just matter of personal preference. The new operator takes precedence before the . operator but the visual might suggest the other way round...

Rashack