title: Javascript Prototypes Assignments
body: <p>In javascript , when you assign one object to another you in fact create a reference or an alias , two names pointing to the same thing . At least that's the way it is in this case:</p>
  
  <pre><code>var obja = { val : 0 };
  var objb = obja;
  objb.val =20;
  obja  //Object {val: 20}
  </code></pre>
  
  <p>However , in prototypical inheritance, that (the object assignment) is one of the ways we're supposed to create inheritance . Taken from Object Oriented Javascript (Stoyan Stefanov) :</p>
  
  <pre><code>function Shape(){}
  // augment prototype
  Shape.prototype.name = 'shape';
  Shape.prototype.toString = function() {return this.name;};
  function TwoDShape(){}
  // take care of inheritance
  TwoDShape.prototype = Shape.prototype;
  TwoDShape.prototype.constructor = TwoDShape;
  // augment prototype
  TwoDShape.prototype.name = '2D shape';
  function Triangle(side, height) {
  this.side = side;
  this.height = height;
  }
  // take care of inheritance
  Triangle.prototype = TwoDShape.prototype;
  Triangle.prototype.constructor = Triangle;
  Triangle.prototype.name = 'Triangle';
  </code></pre>
  
  <p>The question is - why do these assignments work they way they do ? If I look at the obja and objb assignment from earlier , I would expect that this : </p>
  
  <pre><code>TwoDShape.prototype = Shape.prototype;
  </code></pre>
  
  <p>Would cause TwoDShape and Shape to have the same exact prototype (both reference the same thing) . Then when TwoDShape starts adding properties to his prototype I would expect them to be added to Shape , while this obviously isn't happening .</p>