<script>   
function Retangle(w,h){
   this.width=w;
   this.height=h;
}Retangle.prototype.area=function(){return this.width*this.height}function PositionRetangle(x,y,w,h){
   this.superclass(w,h);
   this.x=x
   this.y=y
}
PositionRetangle.prototype.superclass=Retangle
a=new PositionRetangle(4,9,5,7)
alert(a.area())</script>   
怎么让a 弹出面积值35啊? 少写了什么吗

解决方案 »

  1.   

    <script type="text/javascript">
    function Retangle(w,h){
       this.width=w;
       this.height=h;
       Retangle.prototype.area=function(){return this.width*this.height};
    }function PositionRetangle(x,y,w,h){
       this.superclass=new Retangle(w,h);
       this.x=x
       this.y=y
    }
    var a=new PositionRetangle(4,9,5,7);
    alert(a.superclass.area())
    </script>
    不知道行不行
    你的代码真混乱。而且很不规范。。
      

  2.   

    回复x269147836  alert(a.superclass.area()) 可以,但是我想让a继承啊  alert(a.area()) 直接这样不行吗
      

  3.   

    <script type="text/javascript">
    function Retangle(w,h){
      this.width=w;
      this.height=h;
      Retangle.prototype.area=function(){return this.width*this.height};
    }function PositionRetangle(x,y){
      this.x=x;
      this.y=y;
    }
    PositionRetangle.prototype=new Retangle(5,7);
    var a=new PositionRetangle(4,9);
    alert(a.area());
    </script>
      

  4.   

    <script type="text/javascript">
    function Retangle(w,h){
      this.width=w;
      this.height=h;
      Retangle.prototype.area=function(){return this.width*this.height};
    }function PositionRetangle(x,y,w,h){
      Retangle.call(this,w,h);
      this.x=x;
      this.y=y;
    }
    PositionRetangle.prototype=new Retangle();
    var a=new PositionRetangle(4,9,5,7);
    alert(a.area());
    </script>
    一起传参的话 就这样~
      

  5.   

    Retangle.call(this,w,h);  这种方法我会了,  我看犀牛书还可以用superclass代替Retangle.call(this,w,h);
    可试着写代码是不行啊?
      

  6.   

    PositionRetangle.prototype=new Retangle();
      

  7.   

    function Retangle(w,h){
      this.width=w;
      this.height=h;
    }Retangle.prototype.area=function(){return this.width*this.height}
    //因为area方法需要的是width、height这两个属性,所以赋上值再调用area方法就直接可以了。
    function PositionRetangle(x,y,w,h){
      this.width=w;
      this.height=h;
      this.x=x
      this.y=y
    }
    //js因为是原型继承,所以想要让PositionRetangle具有area方法,就必须要把
    //prototype定义为一个具体的Retangle对象

    PositionRetangle.prototype=new Retangle();
    a=new PositionRetangle(4,9,5,7)
      

  8.   

    //这样就可以弹出正确的值了
    alert(a.area());
    </script>
      

  9.   

    this.superclass=new Retangle(w,h);同问啊 楼上的几个回答其实是用类的实例作为原型对象的扩展,并非真正意义上的类的继承啊  我也看到这里迷糊的不行,求高手解答啊  呵呵
      

  10.   

    superclass不是js提供的继承方式,不知道你从哪儿看来的
    js没有提供直接的继承方式,要实现继承需要自己写继承方法
    一般可以用prototype属性达到继承的目的,在网上搜一下js的继承,代码大把,还有一些框架也都提供继承的函数
      

  11.   

    LZ应该是想用JS模拟传统OO语言中的继承,从你的代码中可以看到你想:
    1. area() 是从父类继承的
    2. 继承的同时又想访问到父类的方法,类似java中的super.方法名
    function Retangle(w,h){
      this.width=w;
      this.height=h;
      this.area = function() {
    return this.width * this.height;
      }
    } function PositionRetangle(x,y,w,h) {
    this.x = x;
    this.y = y;
    this.width=w;
    this.height=h;
    this.superclass = new Retangle(this.width, this.height);
    } PositionRetangle.prototype = new Retangle() ; var a = new PositionRetangle(1,1,5,7);
    alert(a.area());
    alert(a.superclass.area());JS有自己的继承机制,推荐你看 《JavaScript高级程序设计》第六章
      

  12.   

    现在基本弄明白了 superclass并不是js的关键字或者保留字, 他就是随便起的一个PositionRetangle.prototype里面的一个属性名,写成别的名字也可以,当构造函数PositionRetangle运行的时候,this.superclass(w,h)这句代码调用了原型方法PositionRetangle.prototype.superclass=Retangle,也就是执行了Retangle()这个函数,PositionRetangle类也就  this.width=w;this.height=h这两个属性值,如果光这么写只继承了this.width和this.height属性,而 Retangle.prototype.area=function(){return this.width*this.height}这个原型方法并没有继承过来,还需要用PositionRetangle.prototype=new Retangle()把原型方法继承过来
     
      

  13.   

    这是我想得到的代码:<script type="text/javascript">
    function Retangle(w,h){
      this.width=w;
      this.height=h;
    }Retangle.prototype.area=function(){return this.width*this.height};function PositionRetangle(x,y,w,h){
      this.superclass(w,h);
      this.x=x
      this.y=y
    }
    PositionRetangle.prototype=new Retangle()
    PositionRetangle.prototype.superclass=Retanglevar a=new PositionRetangle(4,9,5,7);
    alert(a.area())
    </script>
      
      

  14.   

    你这个代码可以运行么?我怀疑Retangle这个构造函数怎么得到你给出的参数?this.superclass(w,h);????建议看看javascript高级程序设计的面向对象部分
      

  15.   

    可用运行的 , 就是通过this.superclass(w,h);   alert(a.area())alert(a.width)
    alert(a.height) alert(a.x) alert(a.y)都可以取值
      

  16.   

    superclass严格说应该叫方法名因为PositionRetangle.prototype.superclass的值是Retangle函数