Getting and Setting Property Descriptors in JavaScript (Part 3)
javascript·@ghasemkiani·
0.000 HBDGetting and Setting Property Descriptors in JavaScript (Part 3)
In the [previous post](https://steemit.com/javascript/@ghasemkiani/getting-and-setting-property-descriptors-in-javascript-part-2), I talked about the meaning of various property attributes. Now let's see how we can define properties with desired attributes. The `Object.defineProperty` and `Object.defineProperties` functions are somehow the opposites of `Object.getOwnPropertydescriptor` and `Object.getOwnPropertydescriptors` functions in that the outputs of the latter functions can be used as the input to the former functions. In the following code snippet, the syntax of `Object.defineProperty` is illustrated: ```javascript let ali = {}; Object.defineProperty(ali, "age", { value: 19, writable: true, enumerable: true, configurable: true, }); Object.defineProperty(ali, "name", { get: function () { if (typeof this._name === "undefined") { this._name = "Ali"; } return this._name; }, set: function (name) { this._name = name; }, enumerable: true, configurable: true, }); ``` Or, you can define all properties at once using `Object.defineProperties`, like this: ```javascript let ali = {}; Object.defineProperties(ali, { age: { value: 19, writable: true, enumerable: true, configurable: true, }, name: { get: function () { if (typeof this._name === "undefined") { this._name = "Ali"; } return this._name; }, set: function (name) { this._name = name; }, enumerable: true, configurable: true }, }); ``` --- ## 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) * [Getting and Setting Property Descriptors in JavaScript (Part 2)](https://steemit.com/javascript/@ghasemkiani/getting-and-setting-property-descriptors-in-javascript-part-2)
👍 ghasemkiani, meme-machine, pornstarshourly, vexedkiller007, upme, rajag234, sam1210, duekie, cryptoeagle, newhope, walnut1, molometer, nameless16, ata27, satyamsharma, lunahod, unnun, votetanding, nimik, sharif24, riezky, munardi, originalworks, sangeeta, delilonia, aismor, relevantsource, lathishkotian, jfernandez, afrizalnt, brkeffect,