Use of javascript object-oriented attribute functions (defineProperty and getOwnProperty Descriptor)

Posted by benjrox on Sun, 02 Jun 2019 23:20:47 +0200

DefneProperty is used to set attribute values and descriptors for an object, with four descriptors: [[Configurable], [[Enumerable], [[Writable], [[Value]]

What does it mean when a property descriptor is set [[Writable]]?

1         "use strict";
2         var person = {};
3         Object.defineProperty( person, "name", {
4             writable : false,
5             value : 'ghostwu'
6         } );
7         console.log( person.name ); //ghostwu
8         person.name = "this is ghostwu";
9         console.log( person.name );//ghostwu
DefneProperty is a person object, adding a name attribute and two attribute descriptors:
  • Writable attribute: false: Writable true: Writable

  • value attribute:'ghostwu'

That is to say, the value of the name attribute is ghostwu, but it cannot be changed. When writing table is false,
If the attribute name is reassigned (modified) in the non-strict mode, the assignment will be ignored and errors will be reported in the strict mode.

When a property descriptor is set [[Configurable]?
 1 //        "use strict";
 2         var person = {};
 3         Object.defineProperty( person, "name", {
 4             configurable : false,
 5             value : 'ghostwu'
 6         } );
 7         console.log( person.name ); //ghostwu
 8         delete person.name;
 9         console.log( person.name );//ghostwu
10 
11         Object.defineProperty( person, "name", {
12             configurable : true,  //Report errors
13             value : 'ghostwu'
14         } );

DefneProperty is a person object, adding a name attribute.

  • Configurable property: false: not configurable true: configurable
  • value attribute:'ghostwu'

That is to say, the value of the name attribute is ghostwu, but it cannot be deleted.
When configurable is false, if name attribute is deleted, deletion operation will be ignored in non-strict mode and error will be reported in strict mode.

And if you want to change a property with configurable as false to true, you will get an error, as in the previous 12 lines.

When an attribute descriptor is set [[Enumerable]]?

 1 //        "use strict";
 2         var person = {};
 3         Object.defineProperty(person, "name", {
 4             enumerable: true,
 5             value: 'ghostwu'
 6         });
 7         Object.defineProperty(person, "sex", {
 8             enumerable: false,
 9             value: 'man'
10         });
11         Object.defineProperty(person, "age", {
12             enumerable: true,
13             value: 22
14         });
15 
16         /*
17             name--->ghostwu
18             age--->22
19         */
20         for( var i in person ){
21             console.log( i + '--->' + person[i] );
22         }

 

DefneProperty is a person object, adding three attributes, name,age,sex. enumerable attribute: false: not enumerable true: enumerable
That is to say, if the value of his enumerable is false, then the attribute cannot be traversed for...in (loop / enumeration).
As shown above, sex is set to be non-enumerable, so for..in will not enumerate it.

Object.getOwnPropertyDescriptor() method: you can get the descriptor of a given attribute. This method supports two parameters, parameter 1: the object where the attribute is located. parameter 2: the name of the attribute. The return value is an object

1         var person = {
2             name : 'ghostwu',
3             age : 22,
4         };
5         var descriptor = Object.getOwnPropertyDescriptor( person, "name" );
6         console.log( descriptor.configurable ); //true
7         console.log( descriptor.enumerable ); //true
8         console.log( descriptor.writable ); //true
9         console.log( descriptor.value ); //ghostwu

For objects that are not defined using Object.defineProperty, their descriptors: configurable, enumerable, and writable are all true by default.

 1         var person2 = {
 2         }
 3         Object.defineProperty( person2, "name", {
 4             value : 'ghostwu'
 5         });
 6 
 7         var descriptor2 = Object.getOwnPropertyDescriptor( person2, "name" );
 8         console.log( descriptor2.configurable ); //false
 9         console.log( descriptor2.enumerable ); //false
10         console.log( descriptor2.writable ); //false
11         console.log( descriptor2.value ); //ghostwu
For objects defined with Object.defineProperty, if there is no value for the configuration descriptor, then configurable, enumerable, and writable default values are false

Topics: Javascript Attribute