Property (JavaScript)
AJavaScript property is a member of anobject that associates a key with a value. A JavaScript object is a data structure that stores a collection of properties.
A property consists of the following parts:
- Aname (also called akey), which is either astring or asymbol.
- Avalue, which can be any JavaScript value. A property that has a function as its value may also be called amethod.
- Someattributes, which specify how the property can be read and written. A property may have the
configurable,enumerable, andwritableattributes.
Accessor properties do not have an actual "value". The value is represented indirectly through a pair of functions, one (the getter) invoked when reading the value and one (the setter) invoked when setting the value. However, accessor properties behave like regular data properties on the surface, because the getter and setter functions are invoked automatically and are typically transparent to JavaScript code.
The property's value (including the getter and setter) and its attributes are stored in a data record called theproperty descriptor. Many methods, such asObject.getOwnPropertyDescriptor() andObject.defineProperty(), work with property descriptors.
The termproperty itself does not correspond to any JavaScript value — it's an abstract concept. For example, in the following code:
const obj = { a: 1, b() {},};The objectobj has two properties. The first one has"a" as the key and1 as the value. The second one has"b" as the key and a function as the value (using themethod syntax). The"a" –1,"b" –function associations are the properties of the object.
In the context ofclasses, properties can be divided intoinstance properties, which are owned by each instance, andstatic properties, which are owned by the class and hold data common to all instances. In the context ofinheritance, properties can also be divided intoown properties, which are owned by the object itself, andinherited properties, which are owned by objects in the prototype chain of the object.
For more information about reading and writing properties, seeworking with objects.