{"id":549,"date":"2018-10-28T11:40:04","date_gmt":"2018-10-28T18:40:04","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=3333"},"modified":"2018-10-28T11:40:04","modified_gmt":"2018-10-28T18:40:04","slug":"proxy-object-always-defined","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=549","title":{"rendered":"Proxy &#8211; (object always defined)"},"content":{"rendered":"<p>This uses a proxy to make sure all keys\/props of an object are always defined.<\/p>\n<pre lang=\"javascript\" line=\"1\">\r\nlet spec = {\r\n  get: (o, key) => {\r\n    return o[key] != null ? o[key] : o[key] = O();\r\n  },\r\n  set: (o, key, v) => {\r\n    o[key] = v;\r\n  }\r\n};\r\n\r\nlet O = () => {\r\n  return new Proxy({}, spec);  \r\n};\r\n\r\nlet dynamic = O();\r\ndynamic.prop.creation = 'is interesting';\r\ndynamic.prop.stuff.not.clear.what.this.could.be.used.for = 123;\r\n\r\n\/\/ log out full structure\r\nlet f = (o) => {\r\n  for (let i in o) {\r\n    console.log(o[i]);\r\n    if (typeof o[i] === 'object') f(o[i]);\r\n  }\r\n};\r\nf(dynamic);\r\n<\/pre>\n<p>Outputs: <\/p>\n<pre>\r\nProxy\u00a0{creation: \"is interesting\", stuff: Proxy}\r\nis interesting\r\nProxy\u00a0{not: Proxy}\r\nProxy\u00a0{clear: Proxy}\r\nProxy\u00a0{what: Proxy}\r\nProxy\u00a0{this: Proxy}\r\nProxy\u00a0{could: Proxy}\r\nProxy\u00a0{be: Proxy}\r\nProxy\u00a0{used: Proxy}\r\nProxy\u00a0{for: 123}\r\n123\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This uses a proxy to make sure all keys\/props of an object are always defined. let spec = { get: (o, key) => { return o[key] != null ? o[key] : o[key] = O(); }, set: (o, key, v) => { o[key] = v; } }; let O = () => { return new Proxy({}, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,192,17,160,7],"tags":[173,197],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-es6","category-functions","category-javascript","category-object","tag-es6","tag-proxy"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=549"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}