12

我想使用具有自定义属性的属性描述符定义一个 JavaScript 属性,换句话说,除了标准之外的属性valuewritable等等......

在下面的示例中,我定义了一个带有自定义属性的属性描述符的属性customAttr。调用Object.defineProperty工作正常,但后来当我尝试遍历属性描述符的属性时,我的自定义属性没有列出。

我正在尝试做的事情可能吗?

const o = {}

Object.defineProperty(o, 'newDataProperty', {
  value: 101,
  writable: true,
  enumerable: true,
  configurable: true,
  customAttr: 1,
})

const desc = Object.getOwnPropertyDescriptor(o, 'newDataProperty')

// List the descriptor attributes.
for (const prop in desc) {
  console.log(`${prop}: ${desc[prop]}`)
}

// PROBLEM: `customAttr` is not listed

4

2 回答 2

8

不,这是不可能的。这是做什么Object.defineProperty的:

...

 3. 设descToPropertyDescriptor为以Attributes作为参数调用的结果。

4.使用参数namedesc和 true调用O 的 [[DefineOwnProperty]] 内部方法。

 5. 返回O

简而言之,ToPropertyDescriptor简单地忽略任何不是“可枚举”、“可写”、“可配置”、“值”、“获取”或“设置”的东西:

  1. ...

  2. desc是创建一个最初没有字段的新属性描述符的结果。

  3. 如果使用参数 " " 调用Obj的 [[HasProperty]] 内部方法的结果enumerable为真,则
    • ...

(对其他有效的描述符属性重复第 3 步)

 10. 返回描述

于 2013-02-28T13:55:31.747 回答
4

在这里复活一个旧帖子,但我发现这个想法很有趣。您可以提取函数是 javascript 中的对象这一事实,并将get函数用作属性持有者:

function setPropertyAttribute(obj, propertyName, attributeName, attributeValue) {
  var descriptor = getCustomPropertyDescriptor(obj, propertyName);

  descriptor.get.$custom[attributeName] = attributeValue;
}

function getPropertyAttributes(obj, propertyName) {
  var descriptor = getCustomPropertyDescriptor(obj, propertyName);

  return descriptor.get.$custom;
}

function getPropertyAttribute(obj, propertyName, attributeName) {
  return getPropertyAttributes(obj, propertyName)[attributeName];
}

function getCustomPropertyDescriptor(obj, prop) {
  var actualDescriptor = Object.getOwnPropertyDescriptor(obj, prop);
  if (actualDescriptor && actualDescriptor.get && actualDescriptor.get.$custom) {
    return actualDescriptor;
  }

  var value = obj[prop];
  var descriptor = {
    get: function() {
      return value;
    },
    set: function(newValue) {
      value = newValue;
    }
  }
  descriptor.get.$custom = {};

  Object.defineProperty(obj, prop, descriptor);
  return Object.getOwnPropertyDescriptor(obj, prop);
}

然后 :

var obj = {
  text: 'value',
  number: 256
}

setPropertyAttribute(obj, 'text', 'myAttribute', 'myAttributeValue');

var attrValue = getPropertyAttribute(obj, 'text', 'myAttribute'); //'myAttributeValue'

在这里摆弄。

于 2017-06-12T02:19:29.577 回答