Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behavior of Object.defineProperty() in JavaScript

Tags:

javascript

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  
like image 576
Ravi Waghmare Avatar asked Apr 19 '19 05:04

Ravi Waghmare


People also ask

How do you define a property on an object in Java?

Object.defineProperty() Jump to: The static method Object.defineProperty() defines a new property directly on an object, or modifies an existing property on an object, and returns the object. Note: You call this method directly on the Object constructor rather than on an instance of type Object.

What happens when the property specified doesn't exist in the object?

When the property specified doesn't exist in the object, Object.defineProperty () creates a new property as described. Fields may be omitted from the descriptor, and default values for those fields are inputted.

What does the static method object defineproperty () do?

The static method Object.defineProperty () defines a new property directly on an object, or modifies an existing property on an object, and returns the object. The source for this interactive example is stored in a GitHub repository.

What is defineproperty () method in Java?

The static method Object.defineProperty () defines a new property directly on an object, or modifies an existing property on an object, and returns the object. The object on which to define the property.


1 Answers

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
like image 74
Maheer Ali Avatar answered Sep 20 '22 22:09

Maheer Ali