title: Creating FabricJS object inside function
body: <p>I don't understand where the problem is exactly. Most likely it's in OOP.</p>
  
  <p>So I create my own class to work with canvas through FabricJS, and create method to add door (I just do cupboard). This method creates fabric.Rect.
  I called this method 3 times and got 3 doors (rects), as it should be.
  But when I try to select one of them canvas returns only last one.</p>
  
  <p>Look at the example <a href="https://jsfiddle.net/Vasek18/w2e17803/" rel="nofollow">jsfiddle</a>, try to select one of the two first doors.</p>
  
  <pre><code>    function Dveri_kupe(options){
      var defaultOptions = {
          doorWidth: 200,
          doorHeight: 400,
          doorDivisionWidth: 5,
          totalWidth: 735,
          totalHeight: 500,
          startOffsetTop: 50,
          startOffsetLeft: 50,
          strokeColor: "#aaa",
          fillStyle: "#ccc",
          lineWidth: "5"
      }
      for(var option in defaultOptions) this[option] = options &amp;&amp; options[option]!==undefined ? options[option] : defaultOptions[option];
  
      var this_ = this;
      this.doorArr = []
  
      canvas = this._canvas = new fabric.Canvas(options.canvasID);
      canvas.on('mouse:down', function(options) {
          if (options.target) {
              console.log('Object. ID = ' + options.target.id + " ", options.target);
          }
      });
  
      this.addDoor = function(options){
          var doorQuan = this.doorArr.length;
  
          console.log("Door add: id = dver" + doorQuan)
  
          this.doorArr[doorQuan] = new fabric.Rect({
              left: this.startOffsetLeft + this.doorWidth * doorQuan,
              top: this.startOffsetTop,
              fill: this.fillStyle,
              width: this.doorWidth,
              height: this.doorHeight,
              strokeWidth: this.lineWidth,
              stroke: this.strokeColor,
              id: "dver" + doorQuan
          });
  
          canvas.add(this.doorArr[doorQuan]);
  
          return doorQuan;
      }
  
      this.tellMeAStory = function(){
          console.log(canvas)
          console.log(this.doorArr)
      }
  };
  
  var dveri_kupe;
  window.onload = function(){
      dveri_kupe = new Dveri_kupe({canvasID : "shkaf"});
      var doorQuan = 3;
      for(var i = 0; i &lt; doorQuan; i++){
          // console.log(dveri_kupe.addDoor())
          dveri_kupe.addDoor()
      }
  }
  </code></pre>
  
  <p>Thanks</p>