Default Values of Property Descriptors in JavaScript

in #javascript7 years ago

When you create a property on an object using the assignment operator or the object literal format, the JavaScript engine creates a property that is writable, configurable, and enumerable, by default.

    let ali = {};
    ali.age = 19;
    
    let susan = {
        age: 14,
    };
    
    console.log(Object.getOwnPropertyDescriptor(ali, "age"));
    console.log(Object.getOwnPropertyDescriptor(susan, "age"));

This is what we get from the last two lines of the code above:

    {
        value: 19,
        writable: true,
        enumerable: true,
        configurable: true,
    }

    {
        value: 14,
        writable: true,
        enumerable: true,
        configurable: true,
    }

You might think that the same happens with Object.defineProperty and Object.defineProperties, too. But this is not the case. The default value of the writable, enumerable, and configurable attributes, when using these functions, is false. If you need the flag to be set, you have to provide the true value yourself.

Here is an example to illustrate this fact:

    let susan = {};
    Object.defineProperty(susan, "age", {
        value: 14,
    });
    
    console.log(Object.getOwnPropertyDescriptor(susan, "age"));

This is the result from this code:

    {
        value: 14,
        writable: false,
        enumerable: false,
        configurable: false,
    }

As you see, the writable, enumerable, and configurable attributes default to a value of false.


Related Posts

Sort:  
window.alert('Can you please look at my steemit.chat DM please? Thx!');

Thank you. I will reply very soon.

thanks for sharing @ghasemkiani .resteemed this post.

Good description by you ...interesting post regarding java.

ehm, it's JavaScript, not Java! Two different languages! :P

The name "JavaScript" contains the "Java" prefix because of the fame and popularity of Java language these days. Still, there is some similarity - Date object API looks the same like in Java.

I'll tell Brendan Eich when I see him! ;-)

java & java script are two different languages.

Fantastic work ..like

some days ago i start javascript. To me this language seems very difficult.

i like this javascript dear. nice sharing @ghasemkiani

You should see my Python, you might get scared just looking at it! :P

#Wow javascript next post thanx you so much friend @ghasemkiani . This post upvote & resteem done .

a very useful science ..
thanks for this excellent post
I like u @ghasemkiani

Coin Marketplace

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