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?
Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.
Fields are ordinary member variables or member instances of a class. Properties are an abstraction to get and set their values. Properties are also called accessors because they offer a way to change and retrieve a field if you expose a field in the class as private.
Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.
A JavaScript class is a blueprint for creating objects. A class encapsulates data and functions that manipulate data. Unlike other programming languages such as Java and C#, JavaScript classes are syntactic sugar over the prototypal inheritance.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With