title: best design practice to separate common code?
body: <p>I have a card class:</p>
  
  <pre><code>function Card()  {
      this.image = new Image();
      this.x = 0;
      this.y = 400;
  
      this.initialX = 0;
      this.initialY = 0;
  
      this.scaleFactor = 4;
  
      this.setImage = function(ii){            
          this.image.src = ii;            
      };
  
      this.getWidth = function(){        
          if (this.image == null){
              return 0;
          }            
          return this.image.width / this.scaleFactor;        
      }
  
      this.getHeight = function(){                
          if (this.image == null){
              return 0;
          }            
          return this.image.height / this.scaleFactor;        
      }        
  
      this.pointIsInCard = function(mx, my){            
          if (mx &gt;= this.x &amp;&amp; mx &lt;= (this.x + this.getWidth()) &amp;&amp; my &gt;= this.y &amp;&amp; my &lt;= (this.y + this.getHeight()))
          {                
              return true;                        
          }
          else{                
              return false;
          }            
      };        
  };    
  </code></pre>
  
  <p>I then have a deck class:</p>
  
  <pre><code>function Deck(x, y, w, h){    
      this.x = x;
      this.y = y;
  
      this.width = w;
      this.height = h;        
  
      this.cards = [];        
  }
  </code></pre>
  
  <p>I need to add a method in <code>Deck</code> class similar to <code>pointIsInCard</code> instead it will be called <code>pointIsInDeck</code>. The logic will be same i.e to check whether the passed in point falls in the boundary of the object. So seeing this duplication of code I wanted to know what is a good design practice to avoid this duplication? One option I thought of was to extract the method out and create a function for generic object with <code>x</code>, <code>y</code>, <code>width</code>, <code>height</code> but again from OOP principles I thought this method should belong to the class/object. I appreciate any help! Thanks!</p>