| 'use strict' | |
| class Frozen { | |
| constructor() { | |
| Object.freeze(this); | |
| } | |
| bar(...args) { | |
| console.log('bar', ...args); | |
| } | |
| } | |
| let f = new Frozen(); | |
| f.bar('no proxy'); | |
| // prints "bar no proxy" | |
| try { | |
| // With 'use strict', this throws an error | |
| f.bar = () => { | |
| console.log('bar2') | |
| } | |
| } catch (e) { | |
| console.log('Cannot extend'); | |
| // prints "Cannot extend" | |
| } | |
| f = new Proxy(f, { | |
| // Called on f.bar (before function execution) | |
| get: (target, prop) => { | |
| // Select the properties you want to proxy | |
| if (prop !== 'bar') { | |
| return target[prop]; | |
| } | |
| return (...args) => { | |
| console.log('CALLING', prop, ...args) | |
| // prints "CALLING bar with proxy 12 false" | |
| target[prop].call(target, ...args) | |
| } | |
| } | |
| }) | |
| f.bar('with proxy', 12, false); | |
| // prints "bar with proxy 12 false" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment