得不到继承对象的属性

youlong118 2013-03-21 03:48:52
对着网上的例子打的,但是不能获取继承对象的属性



function Animal(){
   this.species = "动物";
  }
  
  function Cat(name,color){
    this.name = name;
    this.color = color;
  }


function extend(Child, Parent) {

    var F = function(){};
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.uber = Parent.prototype;
  }
  
  extend(Cat,Animal);
  var cat1 = new Cat("大毛","黄色");
  alert(cat1.species); //为什么是undefined ??? 书上却说可以得到属性“动物”

...全文
130 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinfucheng 2013-03-21
  • 打赏
  • 举报
回复
子类: function Cat(name,color){   this.name = name;   this.color = color; Animal.call(this) }
xinfucheng 2013-03-21
  • 打赏
  • 举报
回复
你这样继承只能继承父类原型里的属性和方法
functionsub 2013-03-21
  • 打赏
  • 举报
回复
function Animal(){

      this.species = "动物";
  }
  
  function Cat(name,color){
    this.name = name;
    this.color = color;
  }
     
     
    function extend(Child, Parent) {
 
    var F = function(){
              Parent.call(this);
        };
    F.prototype = Parent.prototype;
    Child.prototype = new F();
    Child.prototype.constructor = Child;
    Child.uber = Parent.prototype;
  }
  
  extend(Cat,Animal);
  var cat1 = new Cat("大毛","黄色");
  alert(cat1.species);  //为什么是undefined ??? 书上却说可以得到属性“动物”

87,917

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧