<html>
<script type="text/javascript">
function Animal (){
this.foot=4;
}
Animal.prototype.species="动物";
function Cat(name,color) {
this.name=name;
this.color=color;
}
function extend (child,parent) { //为了完成继承
var c=child.prototype;
var p=parent.prototype;
for(var i in p) {
c[i]=p[i]
}
}
extend(Cat,Animal); //运行该函数     var cat1=new Cat("大毛","黄色");
    alert(cat1.species);
    alert(cat1.foot); //输出为undefined,为什么呢??
</script></html>
写在Animal函数里的属性不能继承,而外面的prototype则可以完成继承??为什么呢???
希望知道的能够帮忙解答,先在此谢过各位了

解决方案 »

  1.   

    foot为Animal类实例的属性,并没有在Animal的prototype里。
      

  2.   

    <html>
    <script type="text/javascript">
    function Animal() {
    this.foot=4;
    }
    Animal.prototype.species="动物";
    function Cat(name,color) {
    this.name=name;
    this.color=color;
    }
    Cat.prototype=new Animal(); //这样继承为什么又实现了呢???
    var cat1=new Cat("大毛","黄色");
    alert(cat1.species);
    alert(cat1.foot);
    </script>

    </html>
      

  3.   

    animal 的foot初始化  必须 调用那个构造函数也就是 new Aninal() 这样后才能初始化
    extend函数里 根本没有调用这个构造函数  
    你要是喜欢继承function extend (child,parent) { //为了完成继承
    //var c=child.prototype;
    //var p=parent.prototype;
    //for(var i in p) {
    //c[i]=p[i]
    //}
    child.prototype = new parent()
    }这样就可以了何必那么麻烦呢
      

  4.   

    这样,你就把Animal的实例做为Cat的prototype,刚才不说了么Animal实例是有foot属性的。
      

  5.   

       回楼上的二位: 
        实现继承的方法俺知道的,我这种方法也有自己的好处。。
        它出问题的原因是什么,这是我想知道的。。    如果说foot没有在Animal的prototype里,那为什么Cat.prototype=new Animal()可以实现呢?
      

  6.   


    你的扩展代码 使用的的 prototype 之间的复制function Animal (){
    this.foot=4;
    }
    Animal.prototype.species="动物";
    alert(Animal.prototype.foot);
    alert(new Animal().foot);你打印出来就明白了
      

  7.   

    谢谢kk3k2005的回复,果然foot不在Animal的prototype里,它怎么实例化后就有foot属性了呢??有没有什么参考资料让俺看看的!!!
    俺是初学的,所以问题比较多,再次谢谢。。
      

  8.   


    Cat.prototype=new Animal() 
    Cat的prototype指向了一个Animal实例Cat.prototype=new Animal(); //这样继承为什么又实现了呢???
    var cat1=new Cat("大毛","黄色");
    alert(cat1.species);
    alert(cat1.foot); //因为Cat的prototype指向了一个Animal实例,所以在对象寻找属性foot时候,js的原型链会一直延伸到Cat.prototype 按照Cat-->Cat.prototype(new Animal) 直到找到foot属性为止