4 Replies Latest reply on Apr 11, 2013 12:18 AM by Vamitul

    Deeply nested object literal woes

    brianp311

      I'm trying to set up a deeply nested object that I can access throughout my script. It usese a mix of arrays, associative arrays, functions and more. I'm declaring at the top of the script in the global space. Here's what it looks like, more or less, with all the syntax:

       

          var myNS = {

                styles: {

                     HEAD1 : "Headline1",

                     HEAD2: "Headline2",

                     ...

                },

               

                authors: {

                     jd: ["John Doe", "His Title", "hisemailaddress.com"],

                     wb: ["Willy Bob", "Some Other Title", "wbemail@.com],

                     ...

                },

       

                types: {

                   type1: {

                      elems: {

                           "String Field1": { text: "Some kind of text", },

                           "SF 2": { text: "replacement text", },

                           "Another string field" : { text: "More text", },

                       },

                   },

                   type2: { ...

               

                }, //end types

               

                func1: function( aParam ) {

                     ///some code

                },

       

                //and so on until the variable declaration ends with a closing bracket and a semicolon

         };

       

      I'm accessing this global beast of an object elsewhere in the code -- in constructors for other objects, object prototypes, private functions in constructors, etc. For example:

           myStory.paragraphs[0].appliedParagraphStyle = myNS.styles.HEAD1;

       

      Unfortunately, when I run the script, I get error code 21 on the "var myNS = { " line, saying undefined is not an object.

       

      I should have avoid the nested structure in the first place, but does anyone know if InDesign CS3 doesn't like nested object literals or if they can see anything in my syntax that might be throwing it for a loop? I've been staring at the dang thing for hours now... probably could have rewritten the structure in that time. Stubborn me.

        • 1. Re: Deeply nested object literal woes
          Vamitul Level 4

          it should work. check your code, use a linter maby?

           

           

          var mnS={
              tt1:'tt1',
              tt2:'tt2',
                  qt1:{
                      q1:['alabala','blabla']
                      }
              };
          
          
          $.writeln(mnS.qt1.q1[1])
          
          

          works.

          • 2. Re: Deeply nested object literal woes
            absqua Level 4

            The ESTK will stop on the first, assignment line—wherever the problem is in your object. "undefined is not an object" means you're trying to access a property of an undefined variable. Maybe you're assigning a string value somewhere without quoting it properly? I see an unclosed quote ("wbemail@.com) in your example, for instance.

             

            Jeff

            • 3. Re: Deeply nested object literal woes
              brianp311 Level 1

              I ended up rewriting the object in the module pattern with an anonymous function, as I realized I was trying to access the styles object later on within the static namespace, which was throwing it for a loop.

               

              So it basically goes like this now:

               

                   var myNS = (function {

                       

                        var ns = {};

                        ns.styles = {

                             HED1: "Headline 1",

                             HED2: "Headline 2",

                             ...

                        };

                        ns.someotherprop = {

                             //some stuff that uses ns.styles.HED1;

                        };

                       

                        return ns;

                   }());

              • 4. Re: Deeply nested object literal woes
                Vamitul Level 4

                Not sure if anonymus function is the best way to go, but if it works for you, then good luck.