Created
February 15, 2018 21:04
-
-
Save un-def/44c2cb8082043204ed6e973d21d1026d to your computer and use it in GitHub Desktop.
proxyMagic
This file contains 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
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