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.
The above is the detailed content of Can I Omit Parentheses When Using the 'new' Operator in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!