JavaScript from page 195 of Stoyan Stefanov Object Oriented JavaScript
<p>Using the code below, taken from page 95 of Stoyan Stefanov`s Object Oriented JavaScript, if you call</p>

<pre><code>var my = new Triangle(5, 10);
 my.toString()
</code></pre>

<p>You get this result. </p>

<pre><code> "shape, 2D shape, Triangle"
</code></pre>

<p>My question relates to the first function (function Shape) in this code.</p>

<p>1) I know what <code>length</code> property normally does, but why is it important in this <code>function Shape</code> in the code <code>result[result.length]</code>.  If the code is returning the array of strings "shape, 2D shape, Triangle", where is it taking the length of the names and what is it doing with the length of the names?</p>

<p>2) Can you please explain (using plain language) what the program is saying with <code>result[result.length]</code>? i.e. having a result inside a result. </p>

<p>Thanks</p>

<pre><code>function Shape(){}
// augment prototype
Shape.prototype.name = 'shape';
Shape.prototype.toString = function(){
var result = [];
if (this.constructor.uber) {
result[result.length] = this.constructor.uber.toString();
}
result[result.length] = this.name;
return result.join(', ');
};


function TwoDShape(){}
// take care of inheritance
var F = function(){};
F.prototype = Shape.prototype;
TwoDShape.prototype = new F();
TwoDShape.prototype.constructor = TwoDShape;
TwoDShape.uber = Shape.prototype;
// augment prototype
TwoDShape.prototype.name = '2D shape';

function Triangle(side, height) {
this.side = side;
this.height = height;
}
// take care of inheritance
var F = function(){};
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
Triangle.uber = TwoDShape.prototype;
// augment prototype
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function(){return this.side * this.height / 2;}
</code></pre>
