Skip to content

Instantly share code, notes, and snippets.

@un-def
Created February 15, 2018 21:04
Show Gist options
  • Save un-def/44c2cb8082043204ed6e973d21d1026d to your computer and use it in GitHub Desktop.
Save un-def/44c2cb8082043204ed6e973d21d1026d to your computer and use it in GitHub Desktop.
proxyMagic
function doMagic(obj) {
return new Proxy(obj, {
construct: (target, args) => {
return doMagic(new target(...args))
},
get: (target, name) => {
let prop = Reflect.get(target, name)
if (typeof prop === 'function') {
return prop.bind(target)
}
return prop
},
})
}
function _doMagic(obj) {
return obj
}
class Pyos {
constructor(name) {
this.name = name
}
woof(sound) {
if (typeof this === 'undefined') {
console.log(`this says "undefined is not a function"`)
} else {
console.log(`${this.name} says "woof!"`)
}
}
}
function executeCallback(callback) {
callback()
}
Pyos = doMagic(Pyos)
let pyos = new Pyos('Sharique')
pyos.woof()
let cb = pyos.woof
executeCallback(cb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment