首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Proxy.handler.getOwnPropertyDescriptor

handler.getOwnPropertyDescriptor()?方法是Object.getOwnPropertyDescriptor()?的陷阱。

语法

代码语言:javascript
复制
var p = new Proxy(target, {
  getOwnPropertyDescriptor: function(target, prop) {
  }
});

参数

下列参数会被传入 getOwnPropertyDescriptor?方法中。这是绑定到处理程序。?

target目标对象。

prop返回属性名称的描述。

返回值

getOwnPropertyDescriptor?方法必须返回一个objectundefined

描述

handler.getOwnPropertyDescriptor()?方法是?Object.getOwnPropertyDescriptor()的陷阱。

拦截

这个陷阱可以拦截这些操作:

  • Object.getOwnPropertyDescriptor()
  • Reflect.getOwnPropertyDescriptor()

不变量

如果下列不变量被违反,代理将抛出一个 TypeError

  • getOwnPropertyDescriptor?必须返回一个?object 或 undefined。

  • 如果属性作为目标对象的不可配置的属性存在,则该属性无法报告为不存在。

  • 如果属性作为目标对象的属性存在,并且目标对象不可扩展,则该属性无法报告为不存在。

  • 如果属性不存在作为目标对象的属性,并且目标对象不可扩展,则不能将其报告为存在。

  • 属性不能被报告为不可配置,如果它不作为目标对象的自身属性存在,或者作为目标对象的可配置的属性存在。

  • Object.getOwnPropertyDescriptor(target)的结果可以使用 Object.defineProperty 应用于目标对象,也不会抛出异常。

示例

以下是Object.getOwnPropertyDescriptor()?的代码陷阱:

代码语言:javascript
复制
var p = new Proxy({ a: 20}, {
  getOwnPropertyDescriptor: function(target, prop) {
    console.log('called: ' + prop);
    return { configurable: true, enumerable: true, value: 10 };
  }
});

console.log(Object.getOwnPropertyDescriptor(p, 'a').value); // "called: a"
                                                            // 10

以下代码则违反了不变量。

代码语言:javascript
复制
var obj = { a: 10 };
Object.preventExtensions(obj);
var p = new Proxy(obj, {
  getOwnPropertyDescriptor: function(target, prop) {
    return undefined;
  }
});

Object.getOwnPropertyDescriptor(p, 'a'); // TypeError is thrown

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of '[GetOwnProperty]' in that specification.

Standard

Initial definition.

ECMAScript Latest Draft (ECMA-262)The definition of '[GetOwnProperty]' in that specification.

Living Standard

?

浏览器兼容性

Feature

Chrome

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

?

18 (18)

?

?

?

Feature

Android

Chrome for Android

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

?

?

18.0 (18)

?

?

?

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com