Skip to content

Instantly share code, notes, and snippets.

@digitalconceptvisuals
Created Jul 30, 2020
Embed
What would you like to do?
// 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