Skip to content

Instantly share code, notes, and snippets.

@digitalconceptvisuals
Created July 30, 2020 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalconceptvisuals/663eb13643a65aefd6114fa57bb2f87b to your computer and use it in GitHub Desktop.
Save digitalconceptvisuals/663eb13643a65aefd6114fa57bb2f87b to your computer and use it in GitHub Desktop.
// Use Proxy object and handler to create a new property on the fly
let Auto = () =>
// Proxy() object of JavaScript
new Proxy({}, {
// Handler checks if prop exits
// otherwise, creates it
get: (obj, prop) =>
prop in obj
? obj[prop]
: obj[prop] = Auto()
}
)
// Create only TOP level
let univ = Auto();
// Now assign to deeper level
univ.college.stream.year = "A+";
// Bingo! It works
console.log(univ);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment