javacript中prototype的实例化_prototype instantiate in javacript

在Javascript高级程序设计中有讲到使用动态原型(dynamic prototyping)来构建对象,其有讲到原型的赋值在判断语句里面不可行,但是其却没有讲根本原因

function Polygon(iSides) {
this.sides = iSides;
if (typeof Polygon._initialized == “undefined”) {
Polygon.prototype.getArea = function () {
return 0;
};
Polygon._initialized = true;
}
}
function Triangle(iBase, iHeight) {
Polygon.call(this, 3);
this.base = iBase;
this.height = iHeight;
if (typeof Triangle._initialized == “undefined”) {
Triangle.prototype = new Polygon();
Triangle.prototype.getArea = function () {
return 0.5 this.base this.height;
};
Triangle._initialized = true;
}
}

其有讲到原因:

The previous code illustrates both Polygon and Triangle defined using dynamic prototyping. The mistake is in the highlighted line, where Triangle.prototype is set. Logically, this is the correct location;
but functionally, it doesn’t work. Technically, by the time that code is run, the object is already instantiated and tied to the original prototype object. Although changes to that prototype object are reflected properly with very late binding, replacing the prototype object has no effect on that object. Only future object instances reflect the change, making the first instance incorrect.

这表明prototype对象是在代码执行之前进行实例化的,有文章说是在解析期进行实例化的,但是个人比较奇怪的是为什么在调用两次才真正让原型赋值成功:

var temp1 = new Triangle(2,3);
var temp2 = new Triangle(4,5);
alert(temp2.getArea());