JS: Property Overview
What is a Property
A property of an Object is a key and value pair, attached to the object.
2 Kinds of Properties
There are 2 kinds of properties.
- Data property
-
This is the property used in 99% of code. e.g.
{a:1, b:2}
- Accessor property (aka Getter/Setter property)
-
Accessor property generates its value dynamically when being accessed or set. (it calls a function implicitly).
〔see Getter Setter Properties〕
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 can be looped-thru via some operation.
〔see Property Attributes〕
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.
〔see Prototype and 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.
Allowed Value Types for Property Key, Property Value
- Property key can be type string or type symbol.
- Property value can be any type.
- When a property value is a function, that property is often called a method.
Accessing Property, Dot Notation vs Bracket Notation
There are 2 syntax to access a object's property:
The dot notation
x.b
and
bracket notation
x["b"]
.
Bracket notation can be used for property string keys that contain a space, and other unusual situations.
Add / Remove Property
Property can be added or removed from object.
Accessing (Reading, Writing, Listing, Check Existence) of 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. See:
JavaScript, Property
- JS: Property Overview
- JS: Property Key
- JS: Property Dot Notation vs Bracket Notation
- JS: Create Property
- JS: Delete Property
- JS: Get Set Property
- JS: Check Property Existence
- JS: Access Property
- JS: List Properties
- JS: for-in Loop
- JS: Enumerable Property
- JS: Property Attributes
- JS: Property Descriptor
- JS: Getter Setter Properties