ES6 Language Specification

Object Property Attributes

6.1.7.1 Property Attributes

The spec makes a distinction between 2 types of object properties: data properties and accessor properties. Most people are very familiar with the basics of data properties but less familiar with the powerful accessor properties. This post will discuss both.

Data properties are the typical properties that users define on objects while accessor properties are a unique way to set and get those properties from outside the function. For instance here’s some code that sums up which are which:

Data properties have 4 special attributes that are built into Javascript: Value, Writable, Enumerable, and Configurable. Let’s run through each of these.

The first attribute to discuss for data properties is ValueIn short, the spec says that this is the value retrieved by a Get access of the property. Building off of the previous code snippet, here’s an example of its use:

The second built in attribute for data properties is the Writable attribute. This attribute when false will prevent the accessor properties’ Set attribute from working. In other words in the following snippet setting firstName through the Set attribute will not change its value…

The third built in attribute for data properties is the Enumerable attribute. There are a number of built-in functions that either ignore or don’t ignore non-enumerable items. Setting Enumerable to false will mask those items for the functions that ignore them.  Enumeration is quite idiosyncratic as some functions will hide it and others won’t. It’s important to note that when using Object.defineProperty() the default is non-enumerable, but when using a standard object declaration then the default is enumerable. This can lead to some confusion or bugs and is therefore helpful to be aware of. The common use case for setting enumeration to false is to hide it from the for-in loop, however, you should be aware that this property can still be accessed through other functions as you will see in the following snippet. The following snippet lists functions that ignore or include non-enumerable items…

The fourth and final built in attribute for data properties is the Configurable attribute. When this is set to false then it isn’t possible to delete the property or change its attributes. The only exceptions are that the Value attribute may be modified (assuming Writable is true) and that Writable may be switched to false (but not vis-versa).

The defaults for Writable, Enumerable, and Configurable are true in the standard form of object declaration. However, when using property descriptors the defaults are false. The following code snippet demonstrates this…

Accessor properties have 4 special attributes that are built into Javascript: Get, Set, Enumerable, and Configurable. The first 2 we have already seen examples of in the first snippet. The last 2, Enumerable and Configurable, have the same effect as they have with data properties. The defaults for Get and Set are undefined.

All of this said, the following are the relevant portions of the spec:

6.1.7: 

An Object is logically a collection of properties. Each property is either a data property, or an accessor property:

  • A data property associates a key value with an ECMAScript language value and a set of Boolean attributes.
  • An accessor property associates a key value with one or two accessor functions, and a set of Boolean attributes. The accessor functions are used to store or retrieve an ECMAScript language value that is associated with the property.

6.1.7.1

Attributes are used in this specification to define and explain the state of Object properties. A data property associates a key value with the attributes listed in Table 2.

Table 2 — Attributes of a Data Property
Attribute Name Value Domain Description
[[Value]] Any ECMAScript language type The value retrieved by a get access of the property.
[[Writable]] Boolean If false, attempts by ECMAScript code to change the property’s [[Value]] attribute using [[Set]] will not succeed.
[[Enumerable]] Boolean If true, the property will be enumerated by a for-in enumeration (see 13.7.5). Otherwise, the property is said to be non-enumerable.
[[Configurable]] Boolean If false, attempts to delete the property, change the property to be an accessor property, or change its attributes (other than [[Value]], or changing [[Writable]] to false) will fail.

 

An accessor property associates a key value with the attributes listed in Table 3.

Table 3 — Attributes of an Accessor Property
Attribute Name Value Domain Description
[[Get]] Object | Undefined If the value is an Object it must be a function object. The function’s [[Call]] internal method (Table 6) is called with an empty arguments list to retrieve the property value each time a get access of the property is performed.
[[Set]] Object | Undefined If the value is an Object it must be a function object. The function’s [[Call]] internal method (Table 6) is called with an arguments list containing the assigned value as its sole argument each time a set access of the property is performed. The effect of a property’s [[Set]] internal method may, but is not required to, have an effect on the value returned by subsequent calls to the property’s [[Get]] internal method.
[[Enumerable]] Boolean If true, the property is to be enumerated by a for-in enumeration (see 13.7.5). Otherwise, the property is said to be non-enumerable.
[[Configurable]] Boolean If false, attempts to delete the property, change the property to be a data property, or change its attributes will fail.

 

If the initial values of a property’s attributes are not explicitly specified by this specification, the default value defined in Table 4 is used.

Table 4 — Default Attribute Values
Attribute Name Default Value
[[Value]] undefined
[[Get]] undefined
[[Set]] undefined
[[Writable]] false
[[Enumerable]] false
[[Configurable]] false
Josh Miller

Josh Miller Josh Miller

I’m a full-stack web developer who’s especially enthusiastic about the rapid developments in JavaScript. I’ve created this blog as a medium to share with others a journey of knowledge and discovery.