js学习笔记10
30.构造函数链
function Rectangle(w,h){
this.width=w;
this.height=h;
}
Rectangle.prototype.area=function(){
return this.width * this.height;
}
PositionedRectangle.prototype.superClass=Rectangle;
function PositionedRectangle(x,y,w,h){
this.superClass(w,h); //代替了call();
alert(this.width) //return 10
this.x=x;
this.y=y;
}
31.
function GenericToString(){
this.name="cai";
this.add=function(){this.name+this.sex;};
this.sex="nan";
};
GenericToString.prototype.toString=function(){
var props=[];
for(var name in this){ //循环该对象的所有的属性、属性方法和原型对象方法
if(!this.hasOwnProperty(name))continue;
var value=this[name];
var s=name+":";
switch(typeof value){
case "function":s+="function";
break;
case "object" :if(value instanceof Array) s+="array";
else s+=value.toString();
break;
default:s+=String(value);
break;
}
props.push(s);
}
return props.join(",");
}
var g=new GenericToString();
alert(g.toString());
for(var o in GenericToString.prototype){ //循环该对象的所有的原型方法
alert(GenericToString.prototype[o]);
}