title: How to Add Methods to a Function?
body: <p>There are many ways to call functions in JavaScript, but this just isn't working for me. Could someone please tell me exactly what I'm doing wrong?</p>
  
  <p>I tried prototyping (e.g. <code>gameObject.prototype = {};</code>), but that didn't work for some reason. Now I'm just trying to assign the methods directly within the function, and that isn't even working.</p>
  
  <p>What's wrong with this picture?</p>
  
  <pre><code>function gameObject() {
      this.o = {};
  
      this.setimage = function(i) {
          this.o.img = i;
      };
  
      this.setDimensions = function(w, h) {
          this.o.width = w;
          this.o.height = h;
      };
  
      this.setPosition = function(x, y) {
          this.o.x=x;
          this.o.y=y;
      };
  
      this.create = function() {
          var el = document.createElement("div");
          el.className = "object " + this.o.cname;
          el.style.width = width * this.o.w;
          el.style.height = height * this.o.h;
          el.style.position = "absolute";
          el.style.top = height * this.o.y;
          el.style.left = width * this.o.x;
          map.appendChild(el);
      };
  
      this.setClass = function(c) {
          this.o.cname = c;
      };
  
      return this.o;
  }
  </code></pre>
  
  <p>What I want is something like this:</p>
  
  <pre><code>var d = new gameObject();
  d.setClass("class");
  d.setDimensions(0.8, 0.15);
  </code></pre>
  
  <p>I'm still fairly new to object oriented programming, so I don't even know if my vocabulary is correct. What is it that I'm trying to do and what's the proper way to do it exactly?</p>