I have just seen this in code
var thisYear = (new Date()).getFullYear();
This is cool, as I’ve always done something like that in 2 lines, i.e. create the new object instance and assigned it to a variable, then called the method on it.
Is this new method fine to use everywhere? Any gotchas?
That’s not new, but yes it’s safe. You don’t actually need the parentheses either:
new Date()and(new Date())are both expressions that evaluate to a Date object, which you can freely call methods on.You can even call methods directly on numbers:
In this case you do need the parens.