I'm reading through the ES5 spec and got a bit confused about the following piece:
8.6 The Object Type
... There are two kinds of access for named (non-internal) properties: get and put, corresponding to retrieval and assignment, respectively.
Can someone clarify what this phrase refers to exactly (what is its meaning?)?
I have only seen the named accessor properties get
and set
.
The spec is saying that putting and getting are two actions that can happen to properties. Objects have the internal methods [[Put]]
and [[Get]]
that correspond to those actions.
[[Put]]
is an internal method, present within every object, that stores values in an object's properties. Any time you perform an assignment operation on a property, you cause the environment to perform a [[Put]]
.
[[Set]]
is the internal property of an accessor property descriptor. This is what you define when you define a set
setter function of an accessor property. (Note: [[Set]]
exists on property descriptors, not on objects. Not all properties have a [[Set]]
, but all objects do have a [[Put]]
.)
If you attempt to perform a [[Put]]
on an object property which is an accessor property, that operation will invoke the property's [[Set]]
function, per step 5 of the [[Put]]
algorithm:
If IsAccessorDescriptor(desc) is true, then
- a. Let setter be desc.[[Set]] which cannot be undefined.
- b. Call the [[Call]] internal method of setter providing O as the this value and providing V as the sole argument.
In short, the function [[Set]]
only applies to accessor properties, while [[Put]
can apply to either accessor or data properties.