人生这趟旅程,你可以犯错,可以走岔路,但别忘了自己想要去的地方,和一定要见到的人。
今日学习内容
1、JS 红皮书 P279-282 第九章:代理与反射
今日笔记
1、- ownKeys(): ownKeys()捕获器会在 Object.keys()及类似方法中被调用。对应的反射 API 方法为 Reflect. ownKeys()。
1 2 3 4 5 6 7 8 9
| const myTarget = {}; const proxy = new Proxy(myTarget, { ownKeys(target) { console.log("ownKeys()"); return Reflect.ownKeys(...arguments); }, }); Object.keys(proxy);
|
- 返回值
ownKeys()必须返回包含字符串或符号的可枚举对象。
- 拦截的操作
Object.getOwnPropertyNames(proxy)
Object.getOwnPropertySymbols(proxy)
Object.keys(proxy)
Reflect.ownKeys(proxy)
- 捕获器处理程序参数
target:目标对象。
- 捕获器不变式
返回的可枚举对象必须包含 target 的所有不可配置的自有属性。
如果 target 不可扩展,则返回可枚举对象必须准确地包含自有属性键。
- getPrototypeOf(): getPrototypeOf()捕获器会在 Object.getPrototypeOf()中被调用。对应的反射 API 方法为 Reflect.getPrototypeOf()。
1 2 3 4 5 6 7 8 9
| const myTarget = {}; const proxy = new Proxy(myTarget, { getPrototypeOf(target) { console.log("getPrototypeOf()"); return Reflect.getPrototypeOf(...arguments); }, }); Object.getPrototypeOf(proxy);
|
- 返回值
getPrototypeOf()必须返回对象或 null。
- 拦截的操作
Object.getPrototypeOf(proxy)
Reflect.getPrototypeOf(proxy)
proxy.proto
Object.prototype.isPrototypeOf(proxy)
proxy instanceof Object
- 捕获器处理程序参数
target:目标对象。
- 捕获器不变式
如果 target 不可扩展,则 Object.getPrototypeOf(proxy)唯一有效的返回值就是 Object. getPrototypeOf(target)的返回值。
- setPrototypeOf(): setPrototypeOf()捕获器会在 Object.setPrototypeOf()中被调用。对应的反射 API 方法为 Reflect.setPrototypeOf()。
1 2 3 4 5 6 7 8 9
| const myTarget = {}; const proxy = new Proxy(myTarget, { setPrototypeOf(target, prototype) { console.log("setPrototypeOf()"); return Reflect.setPrototypeOf(...arguments); }, }); Object.setPrototypeOf(proxy, Object);
|
- 返回值
setPrototypeOf()必须返回布尔值,表示原型赋值是否成功。返回非布尔值会被转型为布尔值。
- 拦截的操作
Object.setPrototypeOf(proxy)
Reflect.setPrototypeOf(proxy)
- 捕获器处理程序参数
target:目标对象。
prototype:target 的替代原型,如果是顶级原型则为 null。
- 捕获器不变式
如果 target 不可扩展,则唯一有效的 prototype 参数就是 Object.getPrototypeOf(target)的返回值。
- isExtensible(): isExtensible()捕获器会在 Object.isExtensible()中被调用。对应的反射 API 方法为 Reflect.isExtensible()。
1 2 3 4 5 6 7 8 9
| const myTarget = {}; const proxy = new Proxy(myTarget, { isExtensible(target) { console.log("isExtensible()"); return Reflect.isExtensible(...arguments); }, }); Object.isExtensible(proxy);
|
- 返回值
isExtensible()必须返回布尔值,表示 target 是否可扩展。返回非布尔值会被转型为布尔值。
- 拦截的操作
Object.isExtensible(proxy)
Reflect.isExtensible(proxy)
- 捕获器处理程序参数
target:目标对象。
- 捕获器不变式
如果 target 可扩展,则处理程序必须返回 true。
如果 target 不可扩展,则处理程序必须返回 false。
- preventExtensions(): preventExtensions()捕获器会在 Object.preventExtensions()中被调用。对应的反射 API 方法为 Reflect.preventExtensions()。
1 2 3 4 5 6 7 8 9
| const myTarget = {}; const proxy = new Proxy(myTarget, { preventExtensions(target) { console.log("preventExtensions()"); return Reflect.preventExtensions(...arguments); }, }); Object.preventExtensions(proxy);
|
- 返回值
preventExtensions()必须返回布尔值,表示 target 是否已经不可扩展。返回非布尔值会被转
型为布尔值。
- 拦截的操作
Object.preventExtensions(proxy)
Reflect.preventExtensions(proxy)
- 捕获器处理程序参数
target:目标对象。
- 捕获器不变式
如果 Object.isExtensible(proxy)是 false,则处理程序必须返回 true。
- apply(): apply()捕获器会在调用函数时中被调用。对应的反射 API 方法为 Reflect.apply()。
1 2 3 4 5 6 7 8 9
| const myTarget = () => {}; const proxy = new Proxy(myTarget, { apply(target, thisArg, ...argumentsList) { console.log("apply()"); return Reflect.apply(...arguments); }, }); proxy();
|
- 返回值
返回值无限制。
- 拦截的操作
proxy(…argumentsList)
Function.prototype.apply(thisArg, argumentsList)
Function.prototype.call(thisArg, …argumentsList)
Reflect.apply(target, thisArgument, argumentsList)
- 捕获器处理程序参数
target:目标对象。
thisArg:调用函数时的 this 参数。
argumentsList:调用函数时的参数列表
- 捕获器不变式
target 必须是一个函数对象。
- construct(): construct()捕获器会在 new 操作符中被调用。对应的反射 API 方法为 Reflect.construct()。
1 2 3 4 5 6 7 8 9
| const myTarget = function () {}; const proxy = new Proxy(myTarget, { construct(target, argumentsList, newTarget) { console.log("construct()"); return Reflect.construct(...arguments); }, }); new proxy();
|
- 返回值
construct()必须返回一个对象。
- 拦截的操作
new proxy(…argumentsList)
Reflect.construct(target, argumentsList, newTarget)
- 捕获器处理程序参数
target:目标构造函数。
argumentsList:传给目标构造函数的参数列表。
newTarget:最初被调用的构造函数。
- 捕获器不变式
target 必须可以用作构造函数。