Getting and Setting Property Descriptors in JavaScript (Part 2)

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@ghasemkiani·
0.000 HBD
Getting and Setting Property Descriptors in JavaScript (Part 2)
In the [previous post](https://steemit.com/javascript/@ghasemkiani/getting-and-setting-property-descriptors-in-javascript-part-1), I discussed how to get property descriptors. Now, I am going to tell you how to define a property with a desired property descriptor.

But first, let's see why we would want to do this. Defining properties with property descriptors lets you fine-tune the attributes of the property:

* You can define the enumerability of the property. If the property is **enumerable**, it will be listed in a `for .. in` loop or other iteration mechanisms, such as `Object.keys`. (I don't mean to say the `for .. in` loop and `Object.keys` return the same set of properties. No, they have an important difference. The `for .. in` loop iterates over all properties, whether own or inherited, while `Object.keys` returns only own properties.)
* If a property is **configurable**, you can change its definition or even delete it. Otherwise, these actions are not possible.
* For data properties, the **writable** attribute determines if a value can be assigned to the property. If this flag is set to `false`, assigning a value to the property will have no effect.
* For accessor properties, **get** and **set** attributes determine whether the property is _readable_ and _writable_, respectively.

Defining properties is carried out with `Object.defineProperty` and `Object.defineProperties` functions which will be presented in the [next post](https://steemit.com/javascript/@ghasemkiani/getting-and-setting-property-descriptors-in-javascript-part-3).

---

## Related Posts

* [Object Creation and Manipulation Functions in JavaScript](https://steemit.com/javascript/@ghasemkiani/object-creation-and-manipulation-functions-in-javascript)
* [Property Descriptors in JavaScript](https://steemit.com/javascript/@ghasemkiani/property-descriptors-in-javascript)
* [Getting and Setting Property Descriptors in JavaScript (Part 1)](https://steemit.com/javascript/@ghasemkiani/getting-and-setting-property-descriptors-in-javascript-part-1)
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , ,