课程: JavaScript: Five Advanced Challenges and Concepts

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Using proxy objects to intercept calls to an object or class

Using proxy objects to intercept calls to an object or class - JavaScript教程

课程: JavaScript: Five Advanced Challenges and Concepts

Using proxy objects to intercept calls to an object or class

- [Instructor] Proxy objects allow us to intercept traffic during a call to an object, and then modify the object and its methods if necessary. The MDN page for proxy provides a good starting point for understanding how this works. In this very rudimentary setup, you can see a proxy object takes two parameters, target, which is the original object or class, and handler, which is an object that intercepts the operations of the original object or class. Scrolling further down, there is more built-out example that really shows what's going on here. So we have the target object that has two messages in it, then we have a handler object, and we have the proxy down here. So, proxy3 is set to a new proxy that targets the target, and then applies handler3. So if we go and say proxy3, message1, we go up to the target and output Hello. But if we output message2, you'll see that the handler modifies message2 to return world instead.…

内容