Created
November 20, 2017 16:04
-
-
Save hildjj/1ac6e3d52e4e0d23f6289d73c1840a5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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