大网数据广告活动
Web 标准与前端开发

handler.isExtensible()

· 52CE 我爱测

**handler.isExtensible()** 方法用于拦截对对象的 Object.isExtensible()。

handler.isExtensible() 方法用于拦截对对象的 Object.isExtensible()。

JavaScript Demo: handler.isExtensible()

const monster1 = {
  canEvolve: true,
};

const handler1 = {
  isExtensible(target) {
    return Reflect.isExtensible(target);
  },
  preventExtensions(target) {
    target.canEvolve = false;
    return Reflect.preventExtensions(target);
  },
};

const proxy1 = new Proxy(monster1, handler1);

console.log(Object.isExtensible(proxy1));
// Expected output: true

console.log(monster1.canEvolve);
// Expected output: true

Object.preventExtensions(proxy1);

console.log(Object.isExtensible(proxy1));
// Expected output: false

console.log(monster1.canEvolve);
// Expected output: false

语法

var p = new Proxy(target, {
  isExtensible: function (target) {},
});

参数

下列参数将会被传递给 isExtensible方法。this 绑定在 handler 对象上。

  • target
    • : 目标对象。

返回值

isExtensible方法必须返回一个 Boolean 值或可转换成 Boolean 的值。

描述

handler.isExtensible() 用于拦截对对象的 Object.isExtensible()。

拦截

该方法会拦截目标对象的以下操作:

  • Object.isExtensible()
  • Reflect.isExtensible()

约束

如果违背了以下的约束,proxy 会抛出 TypeError:

  • Object.isExtensible(proxy) 必须同 Object.isExtensible(target) 返回相同值。

示例

以下代码演示Object.isExtensible().

var p = new Proxy(
  {},
  {
    isExtensible: function (target) {
      console.log("called");
      return true; // 也可以 return 1; 等表示为 true 的值
    },
  },
);

console.log(Object.isExtensible(p)); // "called"; outputs true

以下代码演示违反约束的情况。

var p = new Proxy(
  {},
  {
    isExtensible: function (target) {
      return false; // return 0; return NaN 等都会报错
    },
  },
);

Object.isExtensible(p); // TypeError is thrown

规范

浏览器兼容性

参见

  • Proxy
  • Proxy/Proxy
  • Object.isExtensible()
  • Reflect.isExtensible()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。