title: Javascript object get code as string
body: <p>First off, I am sorry if this is a duplicate, but every time I googled for 'object' and 'code' I got tutorial pages.</p>
  
  <p>I want to know if there is any easy way to get the code associated with an object. Something like</p>
  
  <pre><code>function A(){
    this.name = 'Kaiser Sauze';
  }
  a = new A();
  console.log(a.displayCode());
  //OUTPUT
  "function A(){ this.name = 'Kaiser Sauze';}"
  </code></pre>
  
  <p>I want to be able to view the code, modify it and reload the function, all from within the browser.  I wanted to know if there was some way to do this, or if I have to prime the pump by doing something like this:</p>
  
  <pre><code>function A(){
    this.name = 'Kaiser Sauze';
    this.code = "function A(){ this.name = 'Kaiser Sauze';}"
  }
  </code></pre>
  
  <p>then every time the user loads up the text editor to view <code>this.code</code> I connect the onchange to update <code>this.code</code>.</p>
  
  <p>EDIT</p>
  
  <p>turns out yankee suggested a simple solution to this</p>
  
  <pre><code>function A(x){
    this.x = x ;
  }
  console.log(A.toString());
  //OUTPUT
  "function A(x){
    this.x = x ;
  }"
  </code></pre>
  
  <p>but in my implementation the variable 'x' can be a function (actually a complicated object with variables, functions and sub objects which I mix in via a call to dojo.mixin), so what I really want is to know the code when instantiated, something like so</p>
  
  <pre><code>function A(x){
    this.x = x ;
  }
  var a = new A(function(){/*DO SOMETHING*/);
  console.log(a.toString());
  //OUTPUT
  "var a = new A(function(){/*DO SOMETHING*/);"
  </code></pre>
  
  <p>but, as most of you already know, all that gets output is something like "Object". I have almost found a way around this, by putting the initialization in a function like so</p>
  
  <pre><code>function A(x){
    this.x = x ;
  }
  function _A(){
    var a = new A(function(){/*DO SOMETHING*/);
  }
  console.log(_A.toString());
  //OUTPUT
  "function _A(){
    var a = new A(function(){/*DO SOMETHING*/);
  }"
  </code></pre>
  
  <p>but that is confusing, and then I have to go in and start parsing the string which I do not want to do.</p>
  
  <p>EDIT: The reason I ask all of this is b/c I want to make code that is both dynamically executable and highly modular. I am dealing with the canvas. I want the user to be able to click on a, for example, rectangle, view its code, and modify and then load/execute it. I have a series of rules but basically I have a shape class and everything that defines that shape (color, transparency, fills, strokes...) has to get passed as a parameter to the object cosntructor, something like:</p>
  
  <pre><code>rect = new Shape({color : 'rgba(0,0,0,1)' , 
    x : 0 , 
    y : 0 , 
    w : 100 , 
    h : 100 ,
    draw : function() {ctx.fillStyle = this.color;
      ctx.fillRect(this.x,this.y,this.w,this.h);
    }
  });
  </code></pre>
  
  <p>This way the code is automatically modular, I don't have to worry about the color being defined at the top of the page, and then the height being defined half way down the page, and so on. Now the only thing I need is to somehow, pass as a parameter, the entire above string representation of the initialization. I could wrap it in a function and call toString on that, like so</p>
  
  <pre><code>function wrapper(){
    rect = new Shape({color : 'rgba(0,0,0,1)' , 
          x : 0 , 
          y : 0 , 
          w : 100 , 
          h : 100 ,
          draw : function() {ctx.fillStyle = this.color;
            ctx.fillRect(this.x,this.y,this.w,this.h);
          },
          code : wrapper.toString()
        });
    }
  </code></pre>
  
  <p>but then there are two problems. 1) I have to manually remove the <code>function wrapper()</code> and trailing <code>}</code> as well as moving every line to the left by one tab. 2) there is no guarantee that a user will remember to include the wrapper function as it is totally unecessary for purposes of drawing. I am trying to think of a way where the wrapper would seem natural, but I can't think of any. But then again I haven't slept in over 30 hours.</p>