Strange behavior of Object.defineProperty() in JavaScript Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Create GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

What does こした mean?

Protagonist's race is hidden - should I reveal it?

How do I deal with an erroneously large refund?

Simulate round-robin tournament draw

Is there a possibility to generate a list dynamically in Latex?

Why isn't everyone flabbergasted about Bran's "gift"?

All ASCII characters with a given bit count

What were wait-states, and why was it only an issue for PCs?

When does Bran Stark remember Jamie pushing him?

Why is water being consumed when my shutoff valve is closed?

Suing a Police Officer Instead of the Police Department

Was there ever a LEGO store in Miami International Airport?

Is there a verb for listening stealthily?

"Working on a knee"

RIP Packet Format

Is Bran literally the world's memory?

Preserving file and folder permissions with rsync

Bright yellow or light yellow?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Why did Europeans not widely domesticate foxes?

How to translate "red flag" into Spanish?

France's Public Holidays' Puzzle

Why does Java have support for time zone offsets with seconds precision?

Could a cockatrice have parasitic embryos?



Strange behavior of Object.defineProperty() in JavaScript



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Create GUID / UUID in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








42















I was playing with below javascript code. Understanding of Object.defineProperty() and I am facing a strange issue with it. When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct



When I debug the code and evaluate the profile I can see the name & age property in the object
But at the time of output, it only shows the name property






//Code Snippet 
let profile =
name: 'Barry Allen',


// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true
)

console.log(profile)
console.log(profile.age)





Now expected output here should be



name: "Barry Allen", age: 23
23


but I get the output as.
Note that I am able to access the age property defined afterwards.
I am not sure why the console.log() is behaving this way.



name: "Barry Allen"
23









share|improve this question









New contributor




Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


























    42















    I was playing with below javascript code. Understanding of Object.defineProperty() and I am facing a strange issue with it. When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct



    When I debug the code and evaluate the profile I can see the name & age property in the object
    But at the time of output, it only shows the name property






    //Code Snippet 
    let profile =
    name: 'Barry Allen',


    // I added a new property in the profile object.
    Object.defineProperty(profile, 'age',
    value: 23,
    writable: true
    )

    console.log(profile)
    console.log(profile.age)





    Now expected output here should be



    name: "Barry Allen", age: 23
    23


    but I get the output as.
    Note that I am able to access the age property defined afterwards.
    I am not sure why the console.log() is behaving this way.



    name: "Barry Allen"
    23









    share|improve this question









    New contributor




    Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      42












      42








      42


      4






      I was playing with below javascript code. Understanding of Object.defineProperty() and I am facing a strange issue with it. When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct



      When I debug the code and evaluate the profile I can see the name & age property in the object
      But at the time of output, it only shows the name property






      //Code Snippet 
      let profile =
      name: 'Barry Allen',


      // I added a new property in the profile object.
      Object.defineProperty(profile, 'age',
      value: 23,
      writable: true
      )

      console.log(profile)
      console.log(profile.age)





      Now expected output here should be



      name: "Barry Allen", age: 23
      23


      but I get the output as.
      Note that I am able to access the age property defined afterwards.
      I am not sure why the console.log() is behaving this way.



      name: "Barry Allen"
      23









      share|improve this question









      New contributor




      Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I was playing with below javascript code. Understanding of Object.defineProperty() and I am facing a strange issue with it. When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct



      When I debug the code and evaluate the profile I can see the name & age property in the object
      But at the time of output, it only shows the name property






      //Code Snippet 
      let profile =
      name: 'Barry Allen',


      // I added a new property in the profile object.
      Object.defineProperty(profile, 'age',
      value: 23,
      writable: true
      )

      console.log(profile)
      console.log(profile.age)





      Now expected output here should be



      name: "Barry Allen", age: 23
      23


      but I get the output as.
      Note that I am able to access the age property defined afterwards.
      I am not sure why the console.log() is behaving this way.



      name: "Barry Allen"
      23





      //Code Snippet 
      let profile =
      name: 'Barry Allen',


      // I added a new property in the profile object.
      Object.defineProperty(profile, 'age',
      value: 23,
      writable: true
      )

      console.log(profile)
      console.log(profile.age)





      //Code Snippet 
      let profile =
      name: 'Barry Allen',


      // I added a new property in the profile object.
      Object.defineProperty(profile, 'age',
      value: 23,
      writable: true
      )

      console.log(profile)
      console.log(profile.age)






      javascript






      share|improve this question









      New contributor




      Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Apr 19 at 10:32









      Pierre44

      1,3081423




      1,3081423






      New contributor




      Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Apr 19 at 5:36









      Ravi WRavi W

      21327




      21327




      New contributor




      Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Ravi W is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          3 Answers
          3






          active

          oldest

          votes


















          51














          You should set enumerable to true. In Object.defineProperty its false by default. According to MDN.




          enumerable

          true if and only if this property shows up during enumeration of the properties on the corresponding object.
          Defaults to false.




          Non-enumerable means that property will not be shown in Object.keys() or for..in loop neither in console






          let profile = 
          name: 'Barry Allen',


          // I added a new property in the profile object.

          Object.defineProperty(profile , 'age',
          value: 23,
          writable: true,
          enumerable: true
          )
          console.log(profile)
          console.log(profile.age)





          All the properties and methods on prototype object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.



          To get all properties(including non-enumerable)Object​.get​OwnProperty​Names()
          .






          let profile = 
          name: 'Barry Allen',


          // I added a new property in the profile object.

          Object.defineProperty(profile , 'age',
          value: 23,
          writable: true,
          enumerable: false
          )
          for(let key in profile) console.log(key) //only name will be displayed.

          console.log(Object.getOwnPropertyNames(profile)) //You will se age too








          share|improve this answer

























          • I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying enumerable to false).

            – randomSoul
            Apr 19 at 5:48











          • @randomSoul I can't get what you mean.

            – Maheer Ali
            Apr 19 at 5:54







          • 1





            See - pasteboard.co/IaOxMqB.png . I did not set enumerable to true for age, but still it is shown.

            – randomSoul
            Apr 19 at 5:59







          • 7





            @randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.

            – Yong Quan
            Apr 19 at 6:20







          • 3





            @randomSoul That's a debugging feature, not a language feature. If you change the example to use JSON.stringify it will behave consistently, and omit the non-enumerable properties.

            – Mike Caron
            Apr 19 at 16:16


















          14














          By default, properties you define with defineProperty are not enumerable - this means that they will not show up when you iterate over their Object.keys (which is what the snippet console does). (Similarly, the length property of an array does not get displayed, because it's non-enumerable.)



          See MDN:




          enumerable



          true if and only if this property shows up during enumeration of the properties on the corresponding object.



          Defaults to false.




          Make it enumerable instead:






          //Code Snippet 
          let profile =
          name: 'Barry Allen',


          // I added a new property in the profile object.
          Object.defineProperty(profile, 'age',
          value: 23,
          writable: true,
          enumerable: true
          )

          console.log(profile)
          console.log(profile.age)





          The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:



          enter image description here



          See how age is grey-ish, while name is not - this indicates that name is enumerable, and age is not.






          share|improve this answer

























          • Someone give this pasteboard.co/IaOxMqB.png Its showing age property in chrome console. Can you please explain that? Does chrome console works differently?

            – Maheer Ali
            Apr 19 at 6:11







          • 2





            Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like age and __proto__) will be slightly greyed out.

            – CertainPerformance
            Apr 19 at 6:17


















          3














          Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.



          //Code Snippet 
          let profile =
          name: 'Barry Allen',


          // I added a new property in the profile object.
          Object.defineProperty(profile, 'age',
          value: 23,
          writable: true,
          enumerable : true,
          configurable : true
          )

          console.log(profile)
          console.log(profile.age)





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Ravi W is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55757089%2fstrange-behavior-of-object-defineproperty-in-javascript%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            51














            You should set enumerable to true. In Object.defineProperty its false by default. According to MDN.




            enumerable

            true if and only if this property shows up during enumeration of the properties on the corresponding object.
            Defaults to false.




            Non-enumerable means that property will not be shown in Object.keys() or for..in loop neither in console






            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: true
            )
            console.log(profile)
            console.log(profile.age)





            All the properties and methods on prototype object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.



            To get all properties(including non-enumerable)Object​.get​OwnProperty​Names()
            .






            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: false
            )
            for(let key in profile) console.log(key) //only name will be displayed.

            console.log(Object.getOwnPropertyNames(profile)) //You will se age too








            share|improve this answer

























            • I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying enumerable to false).

              – randomSoul
              Apr 19 at 5:48











            • @randomSoul I can't get what you mean.

              – Maheer Ali
              Apr 19 at 5:54







            • 1





              See - pasteboard.co/IaOxMqB.png . I did not set enumerable to true for age, but still it is shown.

              – randomSoul
              Apr 19 at 5:59







            • 7





              @randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.

              – Yong Quan
              Apr 19 at 6:20







            • 3





              @randomSoul That's a debugging feature, not a language feature. If you change the example to use JSON.stringify it will behave consistently, and omit the non-enumerable properties.

              – Mike Caron
              Apr 19 at 16:16















            51














            You should set enumerable to true. In Object.defineProperty its false by default. According to MDN.




            enumerable

            true if and only if this property shows up during enumeration of the properties on the corresponding object.
            Defaults to false.




            Non-enumerable means that property will not be shown in Object.keys() or for..in loop neither in console






            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: true
            )
            console.log(profile)
            console.log(profile.age)





            All the properties and methods on prototype object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.



            To get all properties(including non-enumerable)Object​.get​OwnProperty​Names()
            .






            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: false
            )
            for(let key in profile) console.log(key) //only name will be displayed.

            console.log(Object.getOwnPropertyNames(profile)) //You will se age too








            share|improve this answer

























            • I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying enumerable to false).

              – randomSoul
              Apr 19 at 5:48











            • @randomSoul I can't get what you mean.

              – Maheer Ali
              Apr 19 at 5:54







            • 1





              See - pasteboard.co/IaOxMqB.png . I did not set enumerable to true for age, but still it is shown.

              – randomSoul
              Apr 19 at 5:59







            • 7





              @randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.

              – Yong Quan
              Apr 19 at 6:20







            • 3





              @randomSoul That's a debugging feature, not a language feature. If you change the example to use JSON.stringify it will behave consistently, and omit the non-enumerable properties.

              – Mike Caron
              Apr 19 at 16:16













            51












            51








            51







            You should set enumerable to true. In Object.defineProperty its false by default. According to MDN.




            enumerable

            true if and only if this property shows up during enumeration of the properties on the corresponding object.
            Defaults to false.




            Non-enumerable means that property will not be shown in Object.keys() or for..in loop neither in console






            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: true
            )
            console.log(profile)
            console.log(profile.age)





            All the properties and methods on prototype object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.



            To get all properties(including non-enumerable)Object​.get​OwnProperty​Names()
            .






            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: false
            )
            for(let key in profile) console.log(key) //only name will be displayed.

            console.log(Object.getOwnPropertyNames(profile)) //You will se age too








            share|improve this answer















            You should set enumerable to true. In Object.defineProperty its false by default. According to MDN.




            enumerable

            true if and only if this property shows up during enumeration of the properties on the corresponding object.
            Defaults to false.




            Non-enumerable means that property will not be shown in Object.keys() or for..in loop neither in console






            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: true
            )
            console.log(profile)
            console.log(profile.age)





            All the properties and methods on prototype object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.



            To get all properties(including non-enumerable)Object​.get​OwnProperty​Names()
            .






            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: false
            )
            for(let key in profile) console.log(key) //only name will be displayed.

            console.log(Object.getOwnPropertyNames(profile)) //You will se age too








            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: true
            )
            console.log(profile)
            console.log(profile.age)





            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: true
            )
            console.log(profile)
            console.log(profile.age)





            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: false
            )
            for(let key in profile) console.log(key) //only name will be displayed.

            console.log(Object.getOwnPropertyNames(profile)) //You will se age too





            let profile = 
            name: 'Barry Allen',


            // I added a new property in the profile object.

            Object.defineProperty(profile , 'age',
            value: 23,
            writable: true,
            enumerable: false
            )
            for(let key in profile) console.log(key) //only name will be displayed.

            console.log(Object.getOwnPropertyNames(profile)) //You will se age too






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 19 at 6:22

























            answered Apr 19 at 5:38









            Maheer AliMaheer Ali

            12.5k1128




            12.5k1128












            • I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying enumerable to false).

              – randomSoul
              Apr 19 at 5:48











            • @randomSoul I can't get what you mean.

              – Maheer Ali
              Apr 19 at 5:54







            • 1





              See - pasteboard.co/IaOxMqB.png . I did not set enumerable to true for age, but still it is shown.

              – randomSoul
              Apr 19 at 5:59







            • 7





              @randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.

              – Yong Quan
              Apr 19 at 6:20







            • 3





              @randomSoul That's a debugging feature, not a language feature. If you change the example to use JSON.stringify it will behave consistently, and omit the non-enumerable properties.

              – Mike Caron
              Apr 19 at 16:16

















            • I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying enumerable to false).

              – randomSoul
              Apr 19 at 5:48











            • @randomSoul I can't get what you mean.

              – Maheer Ali
              Apr 19 at 5:54







            • 1





              See - pasteboard.co/IaOxMqB.png . I did not set enumerable to true for age, but still it is shown.

              – randomSoul
              Apr 19 at 5:59







            • 7





              @randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.

              – Yong Quan
              Apr 19 at 6:20







            • 3





              @randomSoul That's a debugging feature, not a language feature. If you change the example to use JSON.stringify it will behave consistently, and omit the non-enumerable properties.

              – Mike Caron
              Apr 19 at 16:16
















            I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying enumerable to false).

            – randomSoul
            Apr 19 at 5:48





            I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying enumerable to false).

            – randomSoul
            Apr 19 at 5:48













            @randomSoul I can't get what you mean.

            – Maheer Ali
            Apr 19 at 5:54






            @randomSoul I can't get what you mean.

            – Maheer Ali
            Apr 19 at 5:54





            1




            1





            See - pasteboard.co/IaOxMqB.png . I did not set enumerable to true for age, but still it is shown.

            – randomSoul
            Apr 19 at 5:59






            See - pasteboard.co/IaOxMqB.png . I did not set enumerable to true for age, but still it is shown.

            – randomSoul
            Apr 19 at 5:59





            7




            7





            @randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.

            – Yong Quan
            Apr 19 at 6:20






            @randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.

            – Yong Quan
            Apr 19 at 6:20





            3




            3





            @randomSoul That's a debugging feature, not a language feature. If you change the example to use JSON.stringify it will behave consistently, and omit the non-enumerable properties.

            – Mike Caron
            Apr 19 at 16:16





            @randomSoul That's a debugging feature, not a language feature. If you change the example to use JSON.stringify it will behave consistently, and omit the non-enumerable properties.

            – Mike Caron
            Apr 19 at 16:16













            14














            By default, properties you define with defineProperty are not enumerable - this means that they will not show up when you iterate over their Object.keys (which is what the snippet console does). (Similarly, the length property of an array does not get displayed, because it's non-enumerable.)



            See MDN:




            enumerable



            true if and only if this property shows up during enumeration of the properties on the corresponding object.



            Defaults to false.




            Make it enumerable instead:






            //Code Snippet 
            let profile =
            name: 'Barry Allen',


            // I added a new property in the profile object.
            Object.defineProperty(profile, 'age',
            value: 23,
            writable: true,
            enumerable: true
            )

            console.log(profile)
            console.log(profile.age)





            The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:



            enter image description here



            See how age is grey-ish, while name is not - this indicates that name is enumerable, and age is not.






            share|improve this answer

























            • Someone give this pasteboard.co/IaOxMqB.png Its showing age property in chrome console. Can you please explain that? Does chrome console works differently?

              – Maheer Ali
              Apr 19 at 6:11







            • 2





              Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like age and __proto__) will be slightly greyed out.

              – CertainPerformance
              Apr 19 at 6:17















            14














            By default, properties you define with defineProperty are not enumerable - this means that they will not show up when you iterate over their Object.keys (which is what the snippet console does). (Similarly, the length property of an array does not get displayed, because it's non-enumerable.)



            See MDN:




            enumerable



            true if and only if this property shows up during enumeration of the properties on the corresponding object.



            Defaults to false.




            Make it enumerable instead:






            //Code Snippet 
            let profile =
            name: 'Barry Allen',


            // I added a new property in the profile object.
            Object.defineProperty(profile, 'age',
            value: 23,
            writable: true,
            enumerable: true
            )

            console.log(profile)
            console.log(profile.age)





            The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:



            enter image description here



            See how age is grey-ish, while name is not - this indicates that name is enumerable, and age is not.






            share|improve this answer

























            • Someone give this pasteboard.co/IaOxMqB.png Its showing age property in chrome console. Can you please explain that? Does chrome console works differently?

              – Maheer Ali
              Apr 19 at 6:11







            • 2





              Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like age and __proto__) will be slightly greyed out.

              – CertainPerformance
              Apr 19 at 6:17













            14












            14








            14







            By default, properties you define with defineProperty are not enumerable - this means that they will not show up when you iterate over their Object.keys (which is what the snippet console does). (Similarly, the length property of an array does not get displayed, because it's non-enumerable.)



            See MDN:




            enumerable



            true if and only if this property shows up during enumeration of the properties on the corresponding object.



            Defaults to false.




            Make it enumerable instead:






            //Code Snippet 
            let profile =
            name: 'Barry Allen',


            // I added a new property in the profile object.
            Object.defineProperty(profile, 'age',
            value: 23,
            writable: true,
            enumerable: true
            )

            console.log(profile)
            console.log(profile.age)





            The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:



            enter image description here



            See how age is grey-ish, while name is not - this indicates that name is enumerable, and age is not.






            share|improve this answer















            By default, properties you define with defineProperty are not enumerable - this means that they will not show up when you iterate over their Object.keys (which is what the snippet console does). (Similarly, the length property of an array does not get displayed, because it's non-enumerable.)



            See MDN:




            enumerable



            true if and only if this property shows up during enumeration of the properties on the corresponding object.



            Defaults to false.




            Make it enumerable instead:






            //Code Snippet 
            let profile =
            name: 'Barry Allen',


            // I added a new property in the profile object.
            Object.defineProperty(profile, 'age',
            value: 23,
            writable: true,
            enumerable: true
            )

            console.log(profile)
            console.log(profile.age)





            The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:



            enter image description here



            See how age is grey-ish, while name is not - this indicates that name is enumerable, and age is not.






            //Code Snippet 
            let profile =
            name: 'Barry Allen',


            // I added a new property in the profile object.
            Object.defineProperty(profile, 'age',
            value: 23,
            writable: true,
            enumerable: true
            )

            console.log(profile)
            console.log(profile.age)





            //Code Snippet 
            let profile =
            name: 'Barry Allen',


            // I added a new property in the profile object.
            Object.defineProperty(profile, 'age',
            value: 23,
            writable: true,
            enumerable: true
            )

            console.log(profile)
            console.log(profile.age)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 19 at 6:16

























            answered Apr 19 at 5:39









            CertainPerformanceCertainPerformance

            101k166393




            101k166393












            • Someone give this pasteboard.co/IaOxMqB.png Its showing age property in chrome console. Can you please explain that? Does chrome console works differently?

              – Maheer Ali
              Apr 19 at 6:11







            • 2





              Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like age and __proto__) will be slightly greyed out.

              – CertainPerformance
              Apr 19 at 6:17

















            • Someone give this pasteboard.co/IaOxMqB.png Its showing age property in chrome console. Can you please explain that? Does chrome console works differently?

              – Maheer Ali
              Apr 19 at 6:11







            • 2





              Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like age and __proto__) will be slightly greyed out.

              – CertainPerformance
              Apr 19 at 6:17
















            Someone give this pasteboard.co/IaOxMqB.png Its showing age property in chrome console. Can you please explain that? Does chrome console works differently?

            – Maheer Ali
            Apr 19 at 6:11






            Someone give this pasteboard.co/IaOxMqB.png Its showing age property in chrome console. Can you please explain that? Does chrome console works differently?

            – Maheer Ali
            Apr 19 at 6:11





            2




            2





            Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like age and __proto__) will be slightly greyed out.

            – CertainPerformance
            Apr 19 at 6:17





            Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like age and __proto__) will be slightly greyed out.

            – CertainPerformance
            Apr 19 at 6:17











            3














            Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.



            //Code Snippet 
            let profile =
            name: 'Barry Allen',


            // I added a new property in the profile object.
            Object.defineProperty(profile, 'age',
            value: 23,
            writable: true,
            enumerable : true,
            configurable : true
            )

            console.log(profile)
            console.log(profile.age)





            share|improve this answer



























              3














              Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.



              //Code Snippet 
              let profile =
              name: 'Barry Allen',


              // I added a new property in the profile object.
              Object.defineProperty(profile, 'age',
              value: 23,
              writable: true,
              enumerable : true,
              configurable : true
              )

              console.log(profile)
              console.log(profile.age)





              share|improve this answer

























                3












                3








                3







                Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.



                //Code Snippet 
                let profile =
                name: 'Barry Allen',


                // I added a new property in the profile object.
                Object.defineProperty(profile, 'age',
                value: 23,
                writable: true,
                enumerable : true,
                configurable : true
                )

                console.log(profile)
                console.log(profile.age)





                share|improve this answer













                Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.



                //Code Snippet 
                let profile =
                name: 'Barry Allen',


                // I added a new property in the profile object.
                Object.defineProperty(profile, 'age',
                value: 23,
                writable: true,
                enumerable : true,
                configurable : true
                )

                console.log(profile)
                console.log(profile.age)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 19 at 5:41









                RK_15RK_15

                6179




                6179




















                    Ravi W is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    Ravi W is a new contributor. Be nice, and check out our Code of Conduct.












                    Ravi W is a new contributor. Be nice, and check out our Code of Conduct.











                    Ravi W is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55757089%2fstrange-behavior-of-object-defineproperty-in-javascript%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Sum ergo cogito? 1 nng

                    419 nièngy_Soadمي 19bal1.5o_g

                    Queiggey Chernihivv 9NnOo i Zw X QqKk LpB