当前位置:网站首页>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 {
}
边栏推荐
- 网络安全漏洞分析与漏洞复现
- 单片机IO口控制12V电压通断,MOS和三极管电路
- PMP practice once a day | don't get lost in the exam -7.13
- MCU IO port controls 12V voltage on and off, MOS and triode circuit
- Maximum product of leetcode/ word length
- 竞赛:糖尿病遗传风险检测挑战赛(科大讯飞)
- 2022 Niuke multi school first problem solving Report
- sparksql 与flinksql 建表 与 连表记录
- Characteristics of EMC EMI beads
- 豪华版h5俄罗斯方块小游戏源码
猜你喜欢

bash-shell 免交互

Melt cloud x chat, create a "stress free social" habitat with sound

【OpenCV】生成透明的PNG图像

Deep browser rendering principles

CarSim simulation quick start (XII) - Driver Model (2)

一键开关机电路
![[Qt5] small software with 5 people randomly selected from the bid evaluation expert base](/img/ca/9f6bd6af45e2113c050edf3a65aaf2.png)
[Qt5] small software with 5 people randomly selected from the bid evaluation expert base

博客搭建九:hugo添加搜索功能

【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样

Es6: arrow function usage
随机推荐
2018年1月西邻雪山自驾游攻略
半桥BUCK电路—记录篇
Sliding screen switching on uniapp supports video and image rotation, similar to Tiktok effect
Blog building 7: Hugo
CarSim simulation quick start (XI) - Driver Model (1)
Sparksql and flinksql create and link table records
ASP. Net core foundation VIII
ASP. Net core foundation V
sparksql 与flinksql 建表 与 连表记录
Opencv+paddle Orc recognize pictures and extract table information
CarSim simulation quick start (XIII) - steering system
MCU IO port controls 12V voltage on and off, MOS and triode circuit
Prescan quick start to proficient in lecture 17, speed curve editor
No super high-rise buildings | new regulations: what information does it reveal that no new buildings above 500 meters should be built?
Can‘t connect to server on ‘IP‘ (60)
New generation cloud native message queue (II)
2022牛客多校第二场解题报告
Simple use of unity queue
[pyqt] pyqt development experience_ How to find events and methods of controls
pyspark更改列顺序存入iceberg数据库