定义原型对象,继承对象.示例代码如下:
- <html>
- <head>
- <TITLE>原型_对象_继承</TITLE>
- <SCRIPT Language = "JavaScript">
- {
- function Circle(x,y,r)
- {
- this.x=x;
- this.y=y;
- this.r=r;
- }
- Circle.prototype.pi = 3.1415926;
- function Circle_circumference()
- {
- return 2*this.pi*this.r;
- }
- Circle.prototype.circumference = Circle_circumference;
- var c= new Circle(0.0, 0.0, 5);
- var p = c.circumference();
- document.write("<br>c圆的周长为Circle(0.0, 0.0, 5):",p);
- document.write("<br>继承");
- function Circle_area()
- {
- return this.pi*this.r*this.r;
- }
- Circle.prototype.area = Circle_area;
- var mji = c.area();
- document.write("<br>c圆的面积为:",mji);
- document.write("<br><br>重定义:不继承Circle(0.0, 0.0, 10);");
- var C3 = new Circle(0.0, 0.0, 10);
- function area()
- {
- return 3*this.pi*this.r*this.r;
- }
- C3.area = area;
- p = C3.circumference();
- mji = C3.area();
- document.write("<br>C3圆的周长为:",p);
- document.write("<br>C3圆的面积为:",mji);
- }
- </SCRIPT>
- </head>
- </html>