MDN states that there are two operators in Javscript that share the highest precedence:
- The left-associative member operator:
foo.bar
- The right-associative new operator:
new Foo()
I usually explicitly separate the two: (new Date()).toString()
But I frequently see both of them combined: new Date().toString()
According to this answer, the reason the second way works is that it's the second operator's associativity that matters when both operators have equal precedence. In this case, the member operator is left associative which means new Date()
is evaluated first.
However, if that's the case, then why does new Date.toString()
fail? After all, new Date
is just syntactic sugar for new Date()
. The above argument says it should work, but it obviously doesn't.
What am I missing?
Dim x As New ...
but I don't believe we can doDim x As New Date().ToString()
. We can doDim x As String = new Date().ToString()
.()
operator will execute the constructor before the.
accesses it's properties.