Home > Web Front-end > JS Tutorial > Is Omitting Parentheses in JavaScript's `new` Operator Valid?

Is Omitting Parentheses in JavaScript's `new` Operator Valid?

Mary-Kate Olsen
Release: 2024-12-22 16:36:11
Original
1001 people have browsed it

Is Omitting Parentheses in JavaScript's `new` Operator Valid?

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();
Copy after login

However, it has come to light that JavaScript allows for an alternative syntax, where parentheses are omitted.

const obj = new Foo;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template