javascript継承小例(動態原型方法)
1223 ワード
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;
// Triangle._initialized Triangle ( java )
if(typeof Triangle._initialized == "undefined"){
// , prototype , prototype, , , prototype , alert(triangle.getArea()) 。
//Triangle.prototype = new Polygon();
Triangle.prototype.getArea = function(){
return 0.5 * this.base * this.height;
}
Triangle._initialized = true;
}
}
function myload(){
// ,Triangle._initialized ,
//alert(triangle._initialized);
var triangle = new Triangle(12 , 4);
//alert(triangle._initialized);
alert(triangle.sides);
alert(triangle.getArea());
}