js里的原型对象和继承



js里的原型对象和继承

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>原型对象和继承</title>
</head>

<body>
<script type=”text/javascript”>
<!–
<!–创建circle函数–>
function circle(x,y,r){
 this.x=x;
 this.y=y;
 this.r=r;
}
<!–创建圆形的原型对象属性p,即所有circle对象的共有属性–>
circle.prototype.p=3.1415926;
<!–创建计算圆形长度的方法,把方法赋给原型对象的属性–>
function circle_circumference(){return 2*this.p*this.r;
      alert(this);
}
circle.prototype.circumference=circle_circumference;
<!–创建原型对象的另一个方法,这次用函数直接量来实现–>
circle.prototype.area=function(){return this.p*this.r*this.r;}
<!–创建circle对象,并实现方法–>
var c=new circle(1,2,3);
var a=c.circumference();
var b=c.area();
<!–输出–>

document.write(a);
document.write(“<br>”);
document.write(b);
–>

</script>
</body>
</html>