Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between class fields and properties in Javascript

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?

like image 686
bifi Avatar asked Feb 24 '19 11:02

bifi


People also ask

What is the difference between field and property in JavaScript?

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.

What is difference between fields and properties?

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.

What is class field in JavaScript?

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.

What is JavaScript class property?

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.


1 Answers

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).

like image 158
T.J. Crowder Avatar answered Oct 19 '22 05:10

T.J. Crowder