Professional JavaScript for Web Developers 第四版学习笔记 CHAPTER 9: PROXIES AND REFLECT

欢欢欢欢 发表于 2021-7-7 07:46

Proxy Fundamentals 324
Creating a Passthrough Proxy 324
Defining Traps 325
Trap Parameters and the Reflect API 326
Trap Invariants 328
Revocable Proxies 329
Utility of the Reflect API 329
  Reflect API vs. Object API 330
  Status Flags 330
  Supplanting Operators with First-Class Functions 331
  Safe Function Application 331
Proxying a Proxy 331
Proxy Considerations and Shortcomings 332
  ’this’ Inside a Proxy 332
  Proxies and Internal Slots 333
Proxy Traps and Reflect Methods 333
get() 333
  Return value 334
  Intercepted operations 334
  Trap handler parameters 334
  Trap invariants 334
set() 334
  Return value 335
  Intercepted operations 335
  Trap handler parameters 335
  Trap invariants 335
has() 335
  Return value 336
  Intercepted operations 336
  Trap handler parameters 336
  Trap invariants 336
defineProperty() 336
  Return value 336
  Intercepted operations 336
  Trap handler parameters 337
  Trap invariants 337
getOwnPropertyDescriptor() 337
  Return value 337
  Intercepted operations 337
  Trap handler parameters 337
  Trap invariants 338
deleteProperty() 338
  Return value 338
  Intercepted operations 338
  Trap handler parameters 338
  Trap invariants 339
ownKeys() 339
  Return value 339
  Intercepted operations 339
  Trap handler parameters 339
  Trap invariants 339
getPrototypeOf() 339
  Return value 340
  Intercepted operations 340
  Trap handler parameters 340
  Trap invariants 340
setPrototypeOf() 340
  Return value 341
  Intercepted operations 341
  Trap handler parameters 341
  Trap invariants 341
isExtensible() 341
  Return value 341
  Intercepted operations 341
  Trap handler parameters 342
  Trap invariants 342
preventExtensions() 342
  Return value 342
  Intercepted operations 342
  Trap handler parameters 342
  Trap invariants 342
apply() 342
  Return value 343
  Intercepted operations 343
  Trap handler parameters 343
  Trap invariants 343
construct() 343
  Return value 343
  Intercepted operations 344
  Trap handler parameters 344
  Trap invariants 344
Proxy Patterns 344
Tracking Property Access 344
Hidden Properties 345
Property Validation 345
Function and Constructor Parameter Validation 346
Data Binding and Observables 347
Summary 348

-------------------------------------------------

Proxy

const target = {
 id: 'target'
};
const handler = {};
const proxy = new Proxy(target, handler);
// The 'id' property will access the same value
console.log(target.id); // target
console.log(proxy.id); // target

-------------------------------------------------

Reflect

const target = {
 foo: 'bar',
 baz: 'qux'
};
const handler = {
 get(trapTarget, property, receiver) {
  let decoration = '';
  if (property === 'foo') {
   decoration = '!!!';
  }
  return Reflect.get(...arguments) + decoration;
 }
};
const proxy = new Proxy(target, handler);
console.log(proxy.foo); // bar!!!
console.log(target.foo); // bar
console.log(proxy.baz); // qux
console.log(target.baz); // qux