A descriptor is used to declare whether an attribute can be written to whether it can delete an object that can enumerate and specify content.
Properties
A descriptor is an object that can have the following key values
Field Name | Description |
getter |
The get syntax binds an object property to a function that will be called when that property is looked up. |
setter |
The set syntax binds an object property to a function to be called when there is an attempt to set that property. |
value |
Describes the value of the specified property, which can be any valid Javascript value (function, object, string...) |
configurable |
Declare that the property can be modified and deleted |
enumerable |
Declare that the property can be enumerated, and the enumerable genus can be traversed by the for...in loop. |
writable |
Declare whether the specified attribute can be rewritten |
Description
A descriptor is a property that describes an object's properties. There are two main types of property descriptors that exist in an object: a data descriptor and an access descriptor. You can use the Object.getOwnPropertyDescriptor()
function to get the corresponding property of an object. Descriptor.
Examples
The following example demonstrates how to use the Object.defineProperty() function to create a property under an object in a javascript via a descriptor.
var language = {}; // Define an empty object language Object.defineProperty(language, 'log', { // Define the log attribute under the language object value : ['CN','EN'], writable : true, enumerable : true, configurable : true })
In the above example we defined a writable, deletable enumerable property.This is the same as the javascript directly defined property.
In other words, the above code is equivalent to the following code.
var language = {}; // Define an empty object language languange.log = ['CN','EN']; // Define the log attribute under the language object