How to change the written enumerable and custom property values ​​of a Javascript object?

From node repl:

foo = { bar: 'baz'}; console.log (Object.getOwnPropertyDescriptor(foo, 'bar')) 

Return value:

 { value: 'baz', writable: true, enumerable: true, configurable: true } 

How do you change the record variable and set it to false? What are these values ​​called? Are they part of ES5.1? Is there more that repl did not output?

+6
source share
3 answers

"How can you change the recordable list and set it to false?"

 Object.defineProperty(foo, 'baz', { enumerable:false, writable:false, configurable:false }); 

There is also Object.defineProperties , which is the same, except that you can set several properties and Object.create , which allow you to create a new object and set its prototype object and its descriptors.

"What are these meanings?"

They are property descriptors.

"Are they part of ES5.1?"

Yes, ES5.

"Is there even more that repl did not output?"

What else are property descriptors? Not.

+13
source

squint: I think there is a small input error in your answer.

Your code:

 Object.defineProperty(foo, 'baz', { enumerable:false, writable:false, configurable:false }); 

but the second argument should be the name of the property, not the value, so the correct code is:

 Object.defineProperty(foo, 'bar', { enumerable:false, writable:false, configurable:false }); 
+1
source

Just wanted to add this to

You can change the attributes the first time you create an object as follows:

 var newObj = Object.defineProperty({}, 'aPropertyName', { enumerable:false, writable:false, configurable:false }); 

You can also change several properties at once:

 var newObj = Object.defineProperties({}, { aPropertyName: {enumerable: false, writable: false, configurable: false}, anotherPropertyName: {enumerable: true, writable: true, configurable: false}, finalPropertyName: {enumerable: true, writable: false, configurable: true}, }); 

And, of course, passing the name of the object using the previous method:

 Object.defineProperties(objectName, { aPropertyName: {enumerable: false, writable: false, configurable: false}, anotherPropertyName: {enumerable: true, writable: true, configurable: false}, finalPropertyName: {enumerable: true, writable: false, configurable: true}, }); 
0
source

Source: https://habr.com/ru/post/919410/


All Articles