JavaScript syntax conventionally necessitates the use of parentheses when creating objects with the "new" operator. However, a peculiar exception exists.
As David Flanagan notes in his seminal work, the ECMAScript standard permits the omission of parentheses when invoking the "new" operator without any arguments. This simplified syntax removes the outer brackets:
// Customarily with parentheses const obj = new Foo(); // Optionally without parentheses (for "new" operator only) const obj = new Foo;
Omitting the parentheses does not alter the object creation process or its functionality. The resulting object will possess the same properties and methods as its parenthesized counterpart.
The use of parentheses is a matter of preference. While the omission is permissible, it is not strongly encouraged. JSLint, a popular code linter, expresses disapproval of this practice, potentially disrupting development.
For clarity and consistency, it is advisable to retain the parentheses even when not strictly required. Doing so enhances readability and conforms to common coding norms.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3