通过原型继承
所有的函数类型的对象都拥有一个原型对象,这个原型对象的内容是当前函数锁创建的所有的实例对象所共享的,因此可以通过原型实现继承
//创建Person类
function Person(name,age){
this.name = name
this.age = age
}
//创建Student类
function Student(){
}
Student.prototype =new Person("张三",13)
console.log(new Student)
通过call函数实现继承
- call(context,args...)
-
- context 想让this指向的对象
- args表示传进去的实参
- call()功能
-
- 改变this指向
- 让函数立即执行
- 实现继承问题
//创建Person类
function Person(name,age){
this.name = name
this.age = age
this.say = function() {
console.log("name=" + this.name + ",age=" + this.age)
}
}
//创建Student类
function Student(){
Person.call(this,"张三",13)
}
let stu = new Student()
stu.say()