Skip to content

Instantly share code, notes, and snippets.

@eiriklv
Last active August 22, 2018 12:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiriklv/df990f91aaefe03be1827e9045ce569a to your computer and use it in GitHub Desktop.
Save eiriklv/df990f91aaefe03be1827e9045ce569a to your computer and use it in GitHub Desktop.
Seatbelts on!
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