简体   繁体   English

为什么在访问一个对象之前将其包装在括号中?

[英]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)() ). 您可能还已经编写了(new Date).getTime() ,它与new Date.getTime()绝对不同(后者具有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. 当然,如果您知道优先规则,括号对您来说就没有意义,但是代码仍然更加明确和易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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