代理与反射-捕获器 API

代理与反射-捕获器 API

一 捕获器 API 列表

1.1 construct()

construct()捕获器会在 new 操作符中被调用。对应的反射 API 方法为 Reflect.construct():

const myTarget = function () {}
const proxy = new Proxy(myTarget, {
  construct(target, argumentsList, newTarget) {
    console.log('construct()')
    return Reflect.construct(...arguments)
  },
})
new proxy() // construct()
  1. 返回值
    construct()必须返回一个对象。
  2. 拦截的操作
    • new proxy(…argumentsList)
    • Reflect.construct(target, argumentsList, newTarget)
  3. 捕获器处理程序参数
    • target:目标构造函数。

1.2 get()

get() 捕获器会在获取属性值的操作中被调用,set() 捕获器会在设置属性值的操作中被调用,has() 捕获器会在 in 操作符中被调用:

const myTarget = {}
const proxy = new Proxy(myTarget, {
  get(target, property, receiver) {
    console.log('get()')
    return Reflect.get(...arguments)
  },
})
proxy.foo // get()
  1. 返回值
    返回值无限制。
  2. 拦截的操作
    • proxy.property
    • proxy[property]
    • Object.create(proxy)[property]
    • Reflect.get(proxy, property, receiver)
  3. 捕获器处理程序参数
    • target:目标对象。
    • property:引用的目标对象上的字符串键属性。 ①
    • receiver:代理对象或继承代理对象的对象。
  4. 捕获器不变式
    如果 target.property 不可写且不可配置,则处理程序返回的值必须与 target.property 匹配。
    如果 target.property 不可配置且[[Get]]特性为 undefined, 处理程序的返回值也必须是 undefined。

1.3 set()

set()捕获器会在设置属性值的操作中被调用。对应的反射 API 方法为 Reflect.set()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  set(target, property, value, receiver) {
    console.log('set()')
    return Reflect.set(...arguments)
  },
})
proxy.foo = 'bar' // set()
  1. 返回值
    返回 true 表示成功;返回 false 表示失败,严格模式下会抛出 TypeError。
  2. 拦截的操作
    • proxy.property = value
    • proxy[property] = value
    • Object.create(proxy)[property] = value
    • Reflect.set(proxy, property, value, receiver)
  3. 捕获器处理程序参数
    • target:目标对象。
    • property:引用的目标对象上的字符串键属性。
    • value:要赋给属性的值。
    • receiver:接收最初赋值的对象。
  4. 捕获器不变式
    如果 target.property 不可写且不可配置,则不能修改目标属性的值。
    如果 target.property 不可配置且[[Set]]特性为 undefined,则不能修改目标属性的值。
    在严格模式下,处理程序中返回 false 会抛出 TypeError。

1.4 has()

has()捕获器会在 in 操作符中被调用。对应的反射 API 方法为 Reflect.has()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  has(target, property) {
    console.log('has()')
    return Reflect.has(...arguments)
  },
})
'foo' in proxy // has()
  1. 返回值
    has()必须返回布尔值,表示属性是否存在。返回非布尔值会被转型为布尔值。
  2. 拦截的操作
    • property in proxy
    • property in Object.create(proxy)
    • with(proxy) {(property);}
    • Reflect.has(proxy, property)
  3. 捕获器处理程序参数
    • target:目标对象。
    • property:引用的目标对象上的字符串键属性。
  4. 捕获器不变式
    如果 target.property 存在且不可配置,则处理程序必须返回 true。
    如果 target.property 存在且目标对象不可扩展,则处理程序必须返回 true。

1.5 defineProperty()

defineProperty()捕获器会在 Object.defineProperty()中被调用。对应的反射 API 方法为

Reflect.defineProperty()const myTarget = {};
const proxy = new Proxy(myTarget, {
defineProperty(target, property, descriptor) {
console.log('defineProperty()');
return Reflect.defineProperty(...arguments)
}
});
Object.defineProperty(proxy, 'foo', { value: 'bar' });// defineProperty()
  1. 返回值
    defineProperty()必须返回布尔值,表示属性是否成功定义。返回非布尔值会被转型为布尔值。
  2. 拦截的操作
    • Object.defineProperty(proxy, property, descriptor)
    • Reflect.defineProperty(proxy, property, descriptor)
  3. 捕获器处理程序参数
    • target:目标对象。
    • property:引用的目标对象上的字符串键属性。
    • descriptor:包含可选的 enumerable、 configurable、 writable、 value、 get 和 set 定义的对象。
  4. 捕获器不变式
    如果目标对象不可扩展,则无法定义属性。
    如果目标对象有一个可配置的属性,则不能添加同名的不可配置属性。
    如果目标对象有一个不可配置的属性,则不能添加同名的可配置属性。

1.6 getOwnPropertyDescriptor()

getOwnPropertyDescriptor()捕获器会在 Object.getOwnPropertyDescriptor()中被调用。对应的反射 API 方法为 Reflect.getOwnPropertyDescriptor()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  getOwnPropertyDescriptor(target, property) {
    console.log('getOwnPropertyDescriptor()')
    return Reflect.getOwnPropertyDescriptor(...arguments)
  },
})
Object.getOwnPropertyDescriptor(proxy, 'foo') // getOwnPropertyDescriptor()
  1. 返回值
    getOwnPropertyDescriptor()必须返回对象,或者在属性不存在时返回 undefined。
  2. 拦截的操作
    • Object.getOwnPropertyDescriptor(proxy, property)
    • Reflect.getOwnPropertyDescriptor(proxy, property)
  3. 捕获器处理程序参数
    • target:目标对象。
    • property:引用的目标对象上的字符串键属性。
  4. 捕获器不变式
    如果自有的 target.property 存在且不可配置,则处理程序必须返回一个表示该属性存在的对象。
    如果自有的 target.property 存在且可配置,则处理程序必须返回表示该属性可配置的对象。
    如果自有的 target.property 存在且 target 不可扩展,则处理程序必须返回一个表示该属性存在的对象。
    如果 target.property 不存在且 target 不可扩展,则处理程序必须返回 undefined 表示该属性不存在。
    如果 target.property 不存在,则处理程序不能返回表示该属性可配置的对象。

1.7 deleteProperty()

deleteProperty()捕获器会在 delete 操作符中被调用。对应的反射 API 方法为 Reflect.deleteProperty()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  deleteProperty(target, property) {
    console.log('deleteProperty()')
    return Reflect.deleteProperty(...arguments)
  },
})
delete proxy.foo // deleteProperty()
  1. 返回值
    deleteProperty()必须返回布尔值,表示删除属性是否成功。返回非布尔值会被转型为布尔值。
  2. 拦截的操作
    • delete proxy.property
    • delete proxy[property]
    • Reflect.deleteProperty(proxy, property)
  3. 捕获器处理程序参数
    • target:目标对象。
    • property:引用的目标对象上的字符串键属性。
  4. 捕获器不变式
    如果自有的 target.property 存在且不可配置,则处理程序不能删除这个属性。

1.8 ownKeys()

ownKeys()捕获器会在 Object.keys()及类似方法中被调用。对应的反射 API 方法为 Reflect.ownKeys()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  ownKeys(target) {
    console.log('ownKeys()')
    return Reflect.ownKeys(...arguments)
  },
})
Object.keys(proxy) // ownKeys()
  1. 返回值
    ownKeys()必须返回包含字符串或符号的可枚举对象。
  2. 拦截的操作
    • Object.getOwnPropertyNames(proxy)
    • Object.getOwnPropertySymbols(proxy)
    • Object.keys(proxy)
    • Reflect.ownKeys(proxy)
  3. 捕获器处理程序参数
    • target:目标对象。
  4. 捕获器不变式
    返回的可枚举对象必须包含 target 的所有不可配置的自有属性。
    如果 target 不可扩展,则返回可枚举对象必须准确地包含自有属性键。

1.9 getPrototypeOf()

getPrototypeOf()捕获器会在 Object.getPrototypeOf()中被调用。对应的反射 API 方法为 Reflect.getPrototypeOf()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  getPrototypeOf(target) {
    console.log('getPrototypeOf()')
    return Reflect.getPrototypeOf(...arguments)
  },
})
Object.getPrototypeOf(proxy) // getPrototypeOf()
  1. 返回值
    getPrototypeOf()必须返回对象或 null。
  2. 拦截的操作
    • Object.getPrototypeOf(proxy)
    • Reflect.getPrototypeOf(proxy)
    • proxy.__proto__
    • Object.prototype.isPrototypeOf(proxy)
    • proxy instanceof Object
  3. 捕获器处理程序参数
    • target:目标对象。
  4. 捕获器不变式
    如果 target 不可扩展,则 Object.getPrototypeOf(proxy)唯一有效的返回值就是 Object.getPrototypeOf(target)的返回值。

1.10 setPrototypeOf()

setPrototypeOf()捕获器会在 Object.setPrototypeOf()中被调用。对应的反射 API 方法为 Reflect.setPrototypeOf()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  setPrototypeOf(target, prototype) {
    console.log('setPrototypeOf()')
    return Reflect.setPrototypeOf(...arguments)
  },
})
Object.setPrototypeOf(proxy, Object) // setPrototypeOf()
  1. 返回值
    setPrototypeOf()必须返回布尔值,表示原型赋值是否成功。返回非布尔值会被转型为布尔值。
  2. 拦截的操作
    • Object.setPrototypeOf(proxy)
    • Reflect.setPrototypeOf(proxy)
  3. 捕获器处理程序参数
    • target:目标对象。
    • prototype: target 的替代原型,如果是顶级原型则为 null。
  4. 捕获器不变式
    如果 target 不可扩展,则唯一有效的 prototype 参数就是 Object.getPrototypeOf(target)
    的返回值。

1.11 isExtensible()

isExtensible()捕获器会在 Object.isExtensible()中被调用。对应的反射 API 方法为 Reflect.isExtensible()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  isExtensible(target) {
    console.log('isExtensible()')
    return Reflect.isExtensible(...arguments)
  },
})
Object.isExtensible(proxy) // isExtensible()
  1. 返回值
    isExtensible()必须返回布尔值,表示 target 是否可扩展。返回非布尔值会被转型为布尔值。
  2. 拦截的操作
    • Object.isExtensible(proxy)
    • Reflect.isExtensible(proxy)
  3. 捕获器处理程序参数
    • target:目标对象。
  4. 捕获器不变式
    如果 target 可扩展,则处理程序必须返回 true。
    如果 target 不可扩展,则处理程序必须返回 false。

1.12 preventExtensions()

preventExtensions()捕获器会在 Object.preventExtensions()中被调用。对应的反射 API 方法为 Reflect.preventExtensions()。

const myTarget = {}
const proxy = new Proxy(myTarget, {
  preventExtensions(target) {
    console.log('preventExtensions()')
    return Reflect.preventExtensions(...arguments)
  },
})
Object.preventExtensions(proxy) // preventExtensions()
  1. 返回值
    preventExtensions()必须返回布尔值,表示 target 是否已经不可扩展。返回非布尔值会被转
    型为布尔值。
  2. 拦截的操作
    • Object.preventExtensions(proxy)
    • Reflect.preventExtensions(proxy)
  3. 捕获器处理程序参数
    • target:目标对象。
  4. 捕获器不变式
    如果 Object.isExtensible(proxy)是 false,则处理程序必须返回 true。

1.13 apply()

apply()捕获器会在调用函数时中被调用。对应的反射 API 方法为 Reflect.apply()。

const myTarget = () => {}
const proxy = new Proxy(myTarget, {
  apply(target, thisArg, ...argumentsList) {
    console.log('apply()')
    return Reflect.apply(...arguments)
  },
})
proxy() // apply()
  1. 返回值
    返回值无限制。
  2. 拦截的操作
    • proxy(…argumentsList)
    • Function.prototype.apply(thisArg, argumentsList)
    • Function.prototype.call(thisArg, …argumentsList)
    • Reflect.apply(target, thisArgument, argumentsList)
  3. 捕获器处理程序参数
    • target:目标对象。
    • thisArg:调用函数时的 this 参数。
    • argumentsList:调用函数时的参数列表
  4. 捕获器不变式
    target 必须是一个函数对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值