A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/has below:

handler.has() - JavaScript | MDN

handler.has()

Baseline Widely available

handler.has() 方法是针对 in 操作符的代理方法。

尝试一下
const handler1 = {
  has(target, key) {
    if (key[0] === "_") {
      return false;
    }
    return key in target;
  },
};

const monster1 = {
  _secret: "easily scared",
  eyeCount: 4,
};

const proxy1 = new Proxy(monster1, handler1);
console.log("eyeCount" in proxy1);
// Expected output: true

console.log("_secret" in proxy1);
// Expected output: false

console.log("_secret" in monster1);
// Expected output: true
语法
var p = new Proxy(target, {
  has: function (target, prop) {},
});
参数

下面是传递给 has() 方法的参数。this 会被绑定至 handler 上。

target

目标对象。

prop

需要检查是否存在的属性。

返回值

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

描述

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

拦截

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

约束

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

示例

下面的代码拦截了 in 操作符。

var p = new Proxy(
  {},
  {
    has: function (target, prop) {
      console.log("called: " + prop);
      return true;
    },
  },
);

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

下面的代码违反了约束。

var obj = { a: 10 };
Object.preventExtensions(obj);
var p = new Proxy(obj, {
  has: function (target, prop) {
    return false;
  },
});

"a" in p; // TypeError is thrown
规范 浏览器兼容性 其他

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4