1

Consider the following code:

var obj = {};
obj.bar = 'w00p!';           //works
obj.foo.bar = 'there it is'; //throws error because bar is not set

Could you ever make obj.foo.bar = work if obj.foo is not set?

Is there an elegant way to set a deep variable in an object in JavaScript? In the same spirit as mkdir's -p option: mkdir -p a/deep/dir

Fergie
  • 5,933
  • 7
  • 38
  • 42
  • 1
    Possibly a duplicate of [How to create 2nd level property with for JS object using bracketss](http://stackoverflow.com/q/20380504/710446) – apsillers Dec 17 '14 at 14:21
  • This question is not a duplicate of the linked questions, since it is addressing the general case of "autovivification for JavaScript", whereas the linked questions are seeking specific solutions for unique cases. – Fergie Dec 18 '14 at 08:24

0 Answers0