javascript @符_JavaScript属性描述符

javascript @符

Any object in JavaScript has a set of properties, and each of these properties has a descriptor.

JavaScript中的任何对象都有一组属性,并且每个属性都有一个描述符

This is an object that defines a property behavior and own properties.

这是一个定义属性行为和自身属性的对象。

Many Object static methods interact with it. Those methods include:

许多对象静态方法与之交互。 这些方法包括:

Here is an example of a property descriptor object:

这是一个属性描述符对象的示例:

{
  value: 'Something'
}

This is the simplest one. value is the property value, in a key-value definition. This key is defined as the object key, when you define this property in an object:

这是最简单的。 value是键值定义中的属性值。 当您在对象中定义此属性时,此key定义为对象键:

{
  breed: {
    value: 'Siberian Husky'
  }
}

Example:

例:

const animal = {}
const dog = Object.create(animal, {
  breed: {
    value: 'Siberian Husky'
  }
});
console.log(dog.breed) //'Siberian Husky'

You can pass additional properties to define each different object property:

您可以传递其他属性来定义每个不同的对象属性:

  • value: the value of the property

    :属性的值

  • writable: true the property can be changed

    可写 :true可以更改属性

  • configurable: if false, the property cannot be removed nor any attribute can be changed, except its value

    可配置的 :如果为false,则不能删除该属性,也不能更改任何属性,但其值除外

  • enumerable: true if the property is enumerable

    枚举 :如果属性是可枚举的,则为true

  • get: a getter function for the property, called when the property is read

    get :属性的getter函数,在读取属性时调用

  • set: a setter function for the property, called when the property is set to a value

    set :属性的setter函数,在将属性设置为值时调用

writable, configurable and enumerable set the behavior of that property. They have a boolean value, and by default those are all false.

writablewritable configurableenumerable设置该属性的行为。 它们具有布尔值,默认情况下均为false

Example:

例:

const animal = {}
const dog = Object.create(animal, {
  breed: {
    value: 'Siberian Husky',
    writable: false
  }
});
console.log(dog.breed) //'Siberian Husky'
dog.breed = 'Pug' //TypeError: Cannot assign to read only property 'breed' of object '#<Object>'

翻译自: https://flaviocopes.com/javascript-property-descriptors/

javascript @符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值