JS: Property Overview
What is a property
A property of an object is a key and value pair, attached to the object.
for basic syntax, see JS: Object (basics)
Allowed value types for property key and property value
Dot notation vs bracket notation
There are 2 syntax to access a object's property:
obj.name→ dot notationobj[name]→ bracket notation
Bracket notation can be used for property string keys that contain a space, or key is Symbol, or eval an expression.
Accessing property
Property value can be changed anytime (unless the
Property Attribute
configurable
is false).
You can also check if a property exists for a object, in its parents, or list a object's own properties.
Add / remove property
Property attributes
- Each property has associated info called attribute.
- Attributes are things like {value, writable, enumerable, configurable}. They specify the property value, and whether it can be changed, or whether it is included in some iteration constructs.
Own property and inherited property
A property can be said to be a object's “own property” or “inherited property”.
- own property
- A property of a object.
- inherited property
- A property of the object's parent or ancestor.
When a property is looked up (e.g. x.color), JavaScript look at the object to see if it has that property, if not, it lookup its parent, and repeat, until a property is found or a parent is null. This is the technical meaning of inheritance.
This is the most important thing about properties. The behavior of many operations on properties, depend on whether the property is the object's own property.
- Operations that create, modify, delete a property are always done to own properties.
- Operations that read a property usually check inherited property, but not always, depending on the operation.
Accessor (getter setter) properties
There is a special kind of property called Accessor Property, also known as Getter/Setter property.
Accessor property generates its value dynamically when being accessed or set. (it calls a function implicitly).