JSPropertyDescriptor 编辑

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 NameDescription
getterThe get syntax binds an object property to a function that will be called when that property is looked up.
setterThe set syntax binds an object property to a function to be called when there is an attempt to set that property.
valueDescribes the value of the specified property, which can be any valid Javascript value (function, object, string...)
configurableDeclare that the property can be modified and deleted
enumerableDeclare that the property can be enumerated, and the enumerable genus can be traversed by the for...in loop.
writableDeclare 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:63 次

字数:2913

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文