当前位置:网站首页>Js继承方法
Js继承方法
2022-07-28 07:04:00 【名字还没想好*】
一、es5继承方法
1、原型链继承
优点:父类方法可以复用
缺点:
1、 父类所有的引用类型(对象数组)会被子类共享,更改一个子类的数据,其他数据会受到影响。
2、子类实例不能给父类构造函数传参
- 代码
/** 原型链继承 */
function Person(){
this.name ='小李'
this.eats =['apple']
this.getName = function(){
console.log(this.eats)
}
}
Person.prototype.get =function(){
console.log(this.name)
}
function Student(){
}
Student.prototype = new Person()
let stu = new Student()
stu.get()
stu.getName()
stu.eats.push('banana')
let stu2 = new Student()
stu2.getName()
- 运行效果

2、构造函数继承
优点:父类的引用类型的数据不会被子类共享,不会互相影响
缺点:子类不能访问父类原型属性上的方法和参数
- 代码
/** 构造函数继承 */
function Person(){
this.name ='小李'
this.eats =['apple']
this.getName = function(){
console.log(this.eats)
}
}
Person.prototype.get =function(){
console.log(this.name)
}
function Student(){
}
function Student(){
Person.call(this)
}
let stu = new Student()
stu.getName()
stu.get() //报错
- 运行效果

3、组合继承
优点:父类可以复用,父类构造函数的引用类型数据不会被共享
缺点:会调用两次父类的构造函数,会有两份一样的属性和方法,会影响性能
- 代码
/** 组合继承 */
function Person(){
this.name ='小李'
this.eats =['apple']
this.getName = function(){
console.log(this.eats)
}
}
Person.prototype.get =function(){
console.log(this.name)
}
function Student(){
Person.call(this)
}
Student.prototype = new Person()
let stu = new Student()
stu.get()
stu.eats.push('banana')
stu.getName()
let stu2 = new Student()
console.log(stu2)
stu2.getName()
- 运行效果

4、寄生组合继承
目前最优的一个继承方法
/** 寄生组合继承 */
function Person(){
this.name ='小李'
this.eats =['apple']
this.getName = function(){
console.log(this.eats)
}
}
Person.prototype.get =function(){
console.log(this.name)
}
function Student(){
Person.call(this)
}
function Fn(){
}
Fn.prototype = Person.prototype
Student.prototype = new Fn()
let stu = new Student()
stu.get()
stu.eats.push('banana')
stu.getName()
let stu2 = new Student()
console.log(stu2)
stu2.getName()
- 运行效果

二、es6继承方法
使用class类的extends继承
class Person{
constructor(){
this.name = '小李'
this.eats =['apple']
this.getName = function(){
console.log(this.eats)
}
}
}
class Stydent extends Person {
}
边栏推荐
- No super high-rise buildings | new regulations: what information does it reveal that no new buildings above 500 meters should be built?
- 2022 Niuke multi school second problem solving Report
- Recommend a fully open source, feature rich, beautiful interface mall system
- Unity中队列(Queue)的简单使用
- Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
- 招贤纳士,GBASE高端人才招募进行中
- [Qt5] small software with 5 people randomly selected from the bid evaluation expert base
- Usage of qcombobox
- PHPUnit在Window下如何配置
- Pyspark writes data to iceberg
猜你喜欢

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

阿里巴巴内部面试资料

客户至上 | 国产BI领跑者,思迈特软件完成C轮融资

网口网络水晶头RJ45、POE接口定义线序

MySQL how to add users and set permissions?

竞赛:糖尿病遗传风险检测挑战赛(科大讯飞)

单片机IO口控制12V电压通断,MOS和三极管电路

Prescan quick start to proficient in lecture 17, speed curve editor

思迈特软件Smartbi完成C轮融资,推动国产BI加速进入智能化时代
![[Qt5] QT small software release](/img/83/9867bd4513caadac6a056c801abe48.png)
[Qt5] QT small software release
随机推荐
ASP. Net core foundation IV
File operation of QT
Prescan quick start to master the road elements of lecture 15
JS candy xiaoxiaole game source code
c语言中函数的介绍(血书20000字!!!!)
Understand the propagation process of EMI electromagnetic interference through five diagrams - the influence of square wave steepness on high-frequency components, the spectrum graph from time sequenc
Get the clicked line number in qtablewidget
Mysql-怎么添加用户和设置权限?
Window 1 - > main window of the application (27)
PHPUnit在Window下如何配置
PostgreSQL is the world's most advanced open source relational database
Kubernetes技术与架构(七)
Tell you step by step what you need to do to apply for PMP? What should I do?
SQL Server查询结果导出到EXCEL表格
招贤纳士,GBASE高端人才招募进行中
Export SQL server query results to excel table
leetcode刷题,我推荐B站这个妹子学霸的视频
bash-shell 免交互
Openstack dashboard configuring public network access
How to set it to pop up the right-click menu