<!--
继承
面向对象
ES5
//构造函数
function Person(){
this.username= 'jack'
}
//原型对象-公共的属性和方法
Person.prototype.say = function(){
}
ES6
class Person{
}
new Person()
面向对象特性
封装性
多态性
一个事物有多种表现形式
let a = 100 //number
a = '100' //string
继承性
继承
现实生活
儿子可以继承父亲的财产
软件世界
重点: 子类继承父类,子类就拥有父类属性和方法
1. 子类
2. 父类
3. 继承内容:属性和方法
构造函数继承
- 在子类的构造函数中调用父类的构造函数,通过call方法改变this指向
Person.call(this)
- 构造函数继承-继承构造函数中属性和方法
拷贝继承
- 遍历父类原型对象,拷贝父类原型对象属性和方法到子类原型对象上
for(let key in Person.prototype){
Student.prototype[key] = Person.prototype[key]
}
- 拷贝继承-继承原型对象上属性和方法
-->
//人类构造函数 - 父类
function Person(name){
this.name = name
this.say = function(){
console.log(this.name,'说话');
}
}
Person.prototype.eat = function(){
console.log('吃饭');
}
//学生类 构造函数 -子类
function Student(num,name){
Person.call(this,name) //1. 构造函数继承
this.num = num
this.readbook = function(){
console.log('读书');
}
}
//拷贝继承- 原型属性和方法
for(let key in Person.prototype){
Student.prototype[key] = Person.prototype[key]
}
let s1 = new Student(1001,'jack')
s1.readbook()
s1.say()
console.log(s1.name);
s1.eat()