Property Descriptors in JavaScript

in #javascript7 years ago (edited)

A property descriptor is an object that describes the attributes of a property. These attributes include the value of the property, whether the value of the property is writable, whether the property is enumerable, and whether the property is configurable.

As you know, there are two types of properties in JavaScript objects: data properties and accessor properties. Data properties are simple properties that do not perform any specific code when their value is read or written. On the other hand, accessor properties have get and set accessor methods that are called when the property is read or written, respectively.

Here is an example of a data property:

    // descriptor for a data property called age
    {
        value: 19,
        writable: true,
        enumerable: true,
        configurable: true,
    }

And this is an example of an accessor property:

    // descriptor for an accessor property called name
    {
        get: function() {
            if(typeof this._name === "undefined") {
                this._name = "Ali";
            }
            return this._name;
        },
        set: function(name) {
            this._name = name;
        },
        enumerable: true,
        configurable: true,
    }

Each property that is defined on a JavaScript object has an associated property descriptor. Property descriptors can be obtained using Object.getOwnPropertyDescriptor and Object.getOwnPropertyDescriptors functions. They can be set using the Object.defineProperty and Object.defineProperties functions. These functions will be discussed in more detail in future posts.


Related Posts

Sort:  

Thanks for sharing ; javascript is amazing .
The basis of many of today's frameworks and languages ​​is javascript .

Thank you. JavaScript is THE language of the web, and the web is the backbone of our life nowadays. JavaScript is available on almost all devices and is mostly backward-compatible to the earliest days of the web. So if you think of your code as an investment, JavaScript is the best language to invest in.

I am not saying it is a solution to all problems, but it is the best option in a large proportion of programming problems.

Dear sir, @ghasemkiani sometimes your posts proved your efforts to make the digital world better for everyone and I really salute your spirit.
Thanks for the efforts you did here.
Best wishes Sir.

Thank you, dear friend. I wish you all the best.

Thanks a lot for sharing a chunk of javascript programming. I believe that in our world PROGRAMMERS are special people. They are blessed with great skills to develop hidden things towards mankind in easy way. I wanted to learn programming but due to other commitments I can't. Anyways I'm surprised that you're too a programmer. Your post must be noticed in Trending section. All the best @ghasemkiani. Stay blessed!

Good info about Java.

Sir, you're a complete Java programmer.
Lots of your coding skills.Coding skills can be increased greatly from every post in your Java.The future of Java programming is very bright.So I hope you will succeed.This post will be very helpful for those who learn java.
Best wishes for you.
Thank you very much for sharing your skills and helpful post with us @ghasemkiani

Although i am not CSC student but i love to do computer related all things. I made a website without knowing programming. :)

you are multi-talented i think and you have much knowledge. impressive work. I do not know about javascript but appreciate your efforts.

i like programming & thanx you so much for sharing .

Programming is out of my mind , want to understand but can't.. 😀😀

r u programmer

I have been programming since the 1980's, but only on a freelance basis for my own works.

ohhhh wow it means that you are the designer of programing. glad to hear that.
MashaALLAH.

Not really, but thanks for the compliment.

just kidding, dont mind.

good skill of your programming. Thanks

o my god 1980

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 56647.53
ETH 2372.20
USDT 1.00
SBD 2.26