-
-
Save digitalconceptvisuals/663eb13643a65aefd6114fa57bb2f87b to your computer and use it in GitHub Desktop.
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
// 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