简体   繁体   中英

Why wrap a new object in parentheses before accessing one of its members?

I did read that this

var time = (new Date()).getTime();

is preferred to this

var time = new Date().getTime();

although the result in both examples is the same. What is the reason why I should wrap a new object in parentheses before accessing any of its members?

Because it's clearer to read.

You could also have written (new Date).getTime() , which is definitely different from new Date.getTime() (which has the meaning new (Date.getTime)() ). Of course, if you know the precendence rules the parentheses are meaningless to you , but the code is still more explicit and better to maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM