What is the difference between class fields and properties in Javascript
Asked Answered
M

2

17

I'm reading class fields proposal for JavaScript. I don't understand why the authors call it 'fields' and not properties.

MDN docs in class article speak about instance properties declared inside constructor and in next section about field declarations declared using new syntax.

What is the difference between the two besides the syntax?

Member answered 24/2, 2019 at 11:1 Comment(3)
Field declarations are a new and experimental piece of syntax sugar for the new ES6 classes.Wellinformed
@JackBashford - Not so much "experimental" at this point. They're Stage 3 and actively being added to JavaScript engines. Public fields are already in V8, shipping in Chrome without any flags.Dispensation
Sorry @T.J.Crowder I was based off the MDN page providedWellinformed
D
18

What is the difference between the two besides the syntax?

There isn't any. A public field is an instance property, just one created with a field definition rather than by assignment. Other than how they're created, they're exactly the same thing.

The term "field" was used so it could cover both public and private (since private fields are not properties).

Dispensation answered 24/2, 2019 at 11:4 Comment(0)
M
1

Your point is right but my side or opinion about class fields are a new feature in javaScript that introduced ECMAScript 2019.

FACT : This new feature is not yet officially part of the language specification and is not standardized in JavaScript. It is only supported by modern web browsers such as Chrome, Safari, and Firefox. Currently, it is at "stage 3," and it may be released at "stage 4" in the future. It is still under construction and will likely become official in the near future.

Highlight : Class field as "data properties" and "behavior/method" that declarer and initialize instance variable and contain also instances of class. They published two types such as public and private.

Masterpiece answered 5/5, 2023 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.