Last active
August 22, 2018 12:16
-
-
Save eiriklv/df990f91aaefe03be1827e9045ce569a to your computer and use it in GitHub Desktop.
Seatbelts on!
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
const { log } = console; | |
const Undefined = new Proxy(function() {}, { | |
get(target, key, receiver) { | |
if (key === 'name') { | |
return 'Undefined'; | |
} | |
return Undefined; | |
}, | |
apply() { | |
return Undefined; | |
}, | |
}); | |
function seatbelt(obj) { | |
return new Proxy(obj, { | |
get(target, key) { | |
const accessedProperty = Reflect.get(target, key); | |
if (typeof accessedProperty === 'object') { | |
return seatbelt(accessedProperty); | |
} else { | |
return accessedProperty ? Reflect.get(target, key) : Undefined; | |
} | |
} | |
}); | |
} | |
const neverUndefined = seatbelt({ | |
foo: 'bar', | |
baz: { | |
qux: 10, | |
}, | |
}); | |
log(neverUndefined.foo) | |
// bar | |
log(neverUndefined.baz.qux) | |
// 10 | |
log(neverUndefined.baz.fjsdf.fds.fds.fd.sfs.df) | |
// [Function: Undefined] | |
log(neverUndefined.b.c.d.e.sa.fd.fd.f.ds.reg.s.fd.sa.s()()()()()()().f.gf.ggf.dgdf.fgd.gfd.fdg()()()().fdfsdfsd) | |
// [Function: Undefined] | |
/** | |
* Why can't I do this??? 🤔 | |
*/ | |
Object.prototype = seatbelt({}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment