Parentheses Omission in Object Creation Using "new" Operator
The use of parentheses when creating objects using the "new" operator has sparked some debate. It is commonly believed that parentheses are obligatory, as seen in code snippets like the following:
const obj = new Foo();
However, it has come to light that JavaScript allows for an alternative syntax, where parentheses are omitted.
const obj = new Foo;
The question arises: is this latter syntax valid and recognized by the ECMAScript standard?
According to the esteemed author David Flanagan, the answer is a resounding yes. JavaScript, in a special case for the "new" operator, provides the option of omitting parentheses if there are no arguments within the function call.
While both syntactical forms are equally valid, omitting parentheses comes with some potential drawbacks. Enthusiasts of the JavaScript linter JSLint may find their code penalized, as it enforces the inclusion of parentheses in such cases.
In the grand scheme of things, it is a matter of personal preference. However, for consistency and to avoid potential lint warnings, it is generally recommended to stick to the more wide-ranging convention.
The above is the detailed content of Is Omitting Parentheses in JavaScript's `new` Operator Valid?. For more information, please follow other related articles on the PHP Chinese website!