Last active
December 21, 2018 20:45
-
-
Save ricardofalasca/c1754910bdda26cadc38791824dca244 to your computer and use it in GitHub Desktop.
An usage of JavaScript Global Object "Proxy" to create a magical object that can receive any method call - useful for Fake/Mocking objects
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
var magicalObj = (function magicalMethod() { | |
return new Proxy(new Object(), { | |
get(target, propertyName, receiver) { | |
const theMethod = (target.propertyName) ? target.propertyName : function () { return true } | |
return function (...args) { return theMethod.apply(this, args) } | |
} | |
}) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment