title: JavaScript - Save object with methods as a string
body: <p>I've been looking around for a way to do this but can't seem to find anything, I have different configuration objects that I need to save as a text in variables for some processing later on, here is a sample:</p>
  
  <p><strong>object:</strong></p>
  
  <pre><code>args.config.config = {
          next: null,
          final:[],
          delimiter: '~', header: false,
          step: function (row) {
              var item = {
                  'line_code': row.data[0][0],
                  'order': row.data[0][1]
              }
              args.config.config.final.push(item);
          },
          complete: function (result) {
              console.log('Reading data completed. Processing.');
              return args.config.config.next(null, args.config.config.final);
          },
          error: function () {
              console.log('There was an error parsing');
          }'
      }
  </code></pre>
  
  <p>I need to save this as a string, so something like:</p>
  
  <pre><code>args.config.config = "{object goes here}";
  </code></pre>
  
  <p>Without putting everything on one giant line or adding break line characters as this will be parsed later to be used in a config, and that will mess things up, any ideas?</p>
  
  <p><strong>UPDATE:</strong>
  So changing them into text may not be the best solution, these configs will be stored in a mongo database, so it may take them as is (I have not tried it yet).</p>
  
  <p>One of the other problems I was running into was that in the config object I had this:</p>
  
  <pre><code>final.push(item)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>return next(null, final)
  </code></pre>
  
  <p>Which will be defined in another file using the config object:</p>
  
  <p><strong>other file:</strong></p>
  
  <pre><code>exports.parse = function(args, next){//next is what I need to call in the config
  
      var final = []; //this is the final referred to in the config object
      ....
      Baby.parse(data, args.config)
  }
  </code></pre>
  
  <p>So the return next(null, final) and final.push(result) have to refer the the var / function in the new file, but I have no idea how to get that to work, that't why I had to add a final array in the config object and a null next function, then assign it like so:</p>
  
  <pre><code>exports.parse = function(args, next){
      args.config.next = next;
      ....
      Baby.parse(data, args.config)
  }
  </code></pre>
  
  <p>the object was calling it with the ugly line:</p>
  
  <pre><code>   return args.config.config.next(null, args.config.config.final);
  </code></pre>
  
  <p>If anyone has a way around this, it would be much appreciated.</p>