首页 > 代码库 > js对象继承

js对象继承

 一般继承是出现的问题

    function people(name,sex){      this.name=name;      this.sex=sex;    }    people.prototype.showname=function(){      alert(this.name);    }    function student(name,sex,job){      people.call(this,name,sex);      this.job=job;    }    student.prototype = people.prototype;//对象赋给对象,就会出现对象的引用,如果子类原型添加一个方法,父类就会受影响    var p1=new people(‘jack‘,32);    var s1=new student(‘jenny‘,24,‘student‘);    console.log(p1);    console.log(s1);

拷贝继承

  function people(name,sex){      this.name=name;      this.sex=sex;    }    people.prototype.showname=function(){      alert(this.name);    }    function student(name,sex,job){      people.call(this,name,sex);//属性继承:调用父类的构造函数      this.job=job;    }    extend(student.prototype,people.prototype);//拷贝继承,利用for in 实现方法的继承    student.prototype.showjob=function(){      alert();    }    function extend(obj1,obj2){      for (var attr in obj2) {        obj1[attr]=obj2[attr];      }    }    var p1=new people(‘jack‘,32);    var s1=new student(‘jenny‘,24,‘student‘);    console.log(p1);    console.log(s1);

 

 

 

js对象继承