new Date.toString() throws an error because . has higher precedence than new Date so the expression becomes (or it is resolved as) (new (Date.toString))(). They might look same but they are evaluated differently!
In short, if you would like to invoke the constructor and chain it with a method in the object, the correct syntax is:
I am so glad you brought this up. I would love to explain this! There is a reason why this works:
And this does not
It's because
new Date.toString()
is not equal tonew Date().toString()
. *There is an extremely subtle difference. They have different precedence. *Check out: developer.mozilla.org/en-US/docs/W...
new Date.toString()
throws an error because.
has higher precedence thannew Date
so the expression becomes (or it is resolved as)(new (Date.toString))()
. They might look same but they are evaluated differently!In short, if you would like to invoke the constructor and chain it with a method in the object, the correct syntax is:
Give it a try😉
That's pretty amazing thanks!
So when chaining you dont save typing the parentheses, they can just go in two different places :)