Reputation: 9
I have seen multiple nodejs examples where
var minute = (new Date() ). getMinutes();
is defined like that. Why is this date object in parenthesis when
var minute = new Date().getMinutes();
works
Upvotes: 0
Views: 209
Reputation: 4180
Lets test it:
// let there be
function someclass()
{
console.log("constructor");
this.method = function()
{
console.log("method");
}
}
// Trying to call different versions
new test().method()
// Output:
// constructor
// method
(new test()).method()
// Output:
// constructor
// method
// just to be sure:
new Date().getTime() === (new Date()).getTime()
// Output:
true
As shown here and mentioned by others, there is no semantic need for the parentheses.
Upvotes: 0
Reputation: 3313
The new
keyword could be ambiguous (to a human) without the parentheses. i.e. is it a new Date()
or is it a new Date().getMinutes()
.
Upvotes: 1