Auto Update and Javascript

Is autovivification used only for "derefencing" undefined structures, because in JavaScript, if you specify an index or property that does not exist, will it not dynamically create it? But isn't this autovivization because you have to declare the base structure as the first object or array?

+7
javascript autovivification
source share
2 answers

Name propagation is one area where autovivitation can be handy in JavaScript. Currently, for the "namespace" of an object, you must do this:

var foo = { bar: { baz: {} } }; foo.bar.baz.myValue = 1; 

If auto-rendering is supported by JavaScript, the first line is not needed. The ability to add arbitrary properties to objects in JavaScript is explained by the fact that it is a dynamic language, but not an auto-vivification.

+13
source share

ES6 Proxy can be used to implement auto-provisioning,

 var tree = () => new Proxy({}, { get: (target, name) => name in target ? target[name] : target[name] = tree() }); 

Test:

 var t = tree(); t.bar.baz.myValue = 1; t.bar.baz.myValue 
+1
source share

All Articles