-
继承
什么是继承:继承就是子类可以使用父类的所有功能,并对其进行扩展
继承的功能:
资源共享
-
继承的应用
下面举例说明,继承的用法
function People(name,height){ //创建一个构造函数
this.name=name;
this.height=height;
}
Animal.prototype.eat=function(){ //将方法原型写在原型中
console.log('输出结果1');
}
function Age(age){ //创建另一个构造函数
this.age=age;
}
Age.prototype= new People();//将People的值(包括方法)替换Age
var people=new People();// 实例化对象
这段程序表示继承的大致用法,下面有一张截图可以说明继承之间的关系
对于继承还有一种更简单的使用继承的方法,如下
function Person(name,sex,age){
this.name=name;
this.sex=sex;
this.age=age;
}
function Tizhong(tz){
this.tz=tz;
}
var ps= new Person();
var tz= new Tizhong();
for(var i in Person){ //使用遍历进行继承属性
tz[i]=ps[i];
}