"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Can I Omit Parentheses When Using the "new" Operator in JavaScript?

Can I Omit Parentheses When Using the "new" Operator in JavaScript?

Published on 2024-12-16
Browse:987

Can I Omit Parentheses When Using the

Omission of Parentheses in Object Creation with the "new" Operator

JavaScript syntax conventionally necessitates the use of parentheses when creating objects with the "new" operator. However, a peculiar exception exists.

Permissible Omission

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;

Implication of Omission

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.

Preference

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.

Personal Recommendation

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.

Latest tutorial More>

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