Sub topic 1
Data Properties vs Accessor Properties
Every property on a JavaScript object is one of two kinds. A data property stores a value. An accessor property stores no value at all. It holds functions that run when you read or write the property. Getters, setters, and much of the class encapsulation covered later in this tutorial rely on this split, so this chapter shows how to see it directly.
Prerequisites
This chapter assumes you are comfortable with:
- Creating objects and reading or assigning properties with dot and bracket notation
- Functions and how
thisis bound when a function is called as a method - ES2015 class syntax and
extends - The basic idea of prototypes and the prototype chain
- What strict mode (
'use strict') changes about silent failures
Two kinds of property
From the outside, both kinds look the same: obj.name reads and obj.name = x writes. The difference is what happens underneath. A data property returns its stored value. An accessor property calls its get function and returns whatever that function returns.
Property descriptors
A property descriptor is a plain object that describes one property's kind and settings. You read it with Object.getOwnPropertyDescriptor.
const point = { x: 1 };
console.log(Object.getOwnPropertyDescriptor(point, 'x'));
// { value: 1, writable: true, enumerable: true, configurable: true }
const box = {
get size() {
return 3;
}
};
console.log(Object.getOwnPropertyDescriptor(box, 'size'));
// { get: [Function: get size], set: undefined, enumerable: true, configurable: true }The x descriptor has value and writable. The size descriptor has get and set instead. The next chapter covers the getter syntax used here. For now, the point is that the two descriptors have different shapes.
| Key | Data property | Accessor property | Meaning |
|---|---|---|---|
| `value` | Yes | No | The stored value |
| `writable` | Yes | No | Whether assignment can change `value` |
| `get` | No | Yes | Function called on read, or `undefined` |
| `set` | No | Yes | Function called on write, or `undefined` |
| `enumerable` | Yes | Yes | Shows up in `for...in`, `Object.keys`, and `JSON.stringify` |
| `configurable` | Yes | Yes | Can be deleted or redefined |
A descriptor cannot be both
A property is exactly one kind. If a descriptor mixes value or writable with get or set, JavaScript throws an error:
const obj = {};
try {
Object.defineProperty(obj, 'bad', {
value: 1,
get() { return 2; }
});
} catch (err) {
console.log(err instanceof TypeError); // true
}Common errors
- Assuming
inorhasOwnPropertytells you the kind. Both returntruefor either kind. Check'value' in descriptorortypeof descriptor.getto find out which kind you have. - Looking for inherited accessors on the instance. Class getters live on the prototype, so
Object.getOwnPropertyDescriptor(instance, 'name')returnsundefined. Look up the descriptor onObject.getPrototypeOf(instance)instead. - Expecting
JSON.stringifyto call prototype getters. It only serializes the object's own enumerable properties. An own enumerable accessor gets called and its result is serialized, but an accessor inherited from a class prototype is skipped. - Forgetting defaults. Properties created with
Object.definePropertydefault tofalseforwritable,enumerable, andconfigurable. Properties created by literals or assignment default totrue.
Exercise
You run Object.getOwnPropertyDescriptor(obj, 'total') and get { get: [Function], set: undefined, enumerable: true, configurable: true }. Is total a data or accessor property, and what does obj.total = 5 do in strict mode?
Show answer
It is an accessor property, because the descriptor has get and set keys and no value or writable. Since set is undefined, the assignment throws a TypeError in strict mode. In sloppy mode it would be silently ignored.
Exercise
Write one line that returns true if the own property key of obj is a data property and false if it is an accessor property.
Show answer
'value' in Object.getOwnPropertyDescriptor(obj, key) returns true for data properties, because only data descriptors have a value key. The line assumes key is an own property. Otherwise the descriptor is undefined and the in check throws.