JSPropertyDescriptor 编辑
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论