Skip to content

Instantly share code, notes, and snippets.

@hildjj
Created November 20, 2017 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hildjj/1ac6e3d52e4e0d23f6289d73c1840a5a to your computer and use it in GitHub Desktop.
Save hildjj/1ac6e3d52e4e0d23f6289d73c1840a5a to your computer and use it in GitHub Desktop.
'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