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

Proxy.handler.has

handler.has()?方法可以看作是针对?in操作的钩子.

语法

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

参数

下面是传递给 has?方法的参数.thisis bound to the handler.

target目标对象.

prop需要检查是否存在的属性.

返回值

has?方法返回一个 boolean 属性的值.

描述

handler.has 方法可以看作是针对 in 操作的钩子.

拦截

这个钩子可以拦截下面这些操作:

  • Property query: foo in proxy
  • Inherited property query: foo in Object.create(proxy)
  • with check: with(proxy) { (foo); }
  • Reflect.has()

不变量

如果违反了下面这些规则, ?proxy 将会抛出?TypeError:

  • 如果目标对象的某一属性本身不可被配置,则该属性不能够被代理隐藏.

  • 如果目标对象为不可扩展对象,则该对象的属性不能够被代理隐藏

示例

下面的代码 hook 了?in操作.

代码语言:javascript
复制
var p = new Proxy({}, {
  has: function(target, prop) {
    console.log('called: ' + prop);
    return true;
  }
});

console.log('a' in p); // "called: a"
                       // true

下面的代码违反了规则.

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

'a' in p; // TypeError is thrown

规范

Specification

Status

Comment

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

Standard

Initial definition.

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

Draft

?

浏览器兼容性

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