登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
23
Star
192
Fork
47
yanleweb
/
interview-question
代码
Issues
1091
Pull Requests
0
Wiki
统计
流水线
服务
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
JS 中继承方式有哪些?
待办的
#I6N4XF
yanleweb
拥有者
创建于
2023-03-14 22:29
# 1、借助构造函数实现继承 call和apply改变的是JS运行的上下文: ```javascript /*借助构造函数实现继承*/ function Parent(name) { this.name = name; this.getName = function () { console.log(this.name); } } function Child(name) { Parent.call(this, name); this.type = 'child1' } let child = new Child('yanle'); child.getName(); console.log(child.type); ``` 父类的this指向到了子类上面去,改变了实例化的this 指向,导致了父类执行的属性和方法,都会挂在到 子类实例上去; 缺点:父类原型链上的东西并没有被继承; # 2、通过原型链实现继承 ```javascript /*通过原型链实现继承*/ function Parent2(){ this.name='parent2' } function Child2(){ this.type='child2' } Child2.prototype=new Parent2(); console.log(new Child2()); ``` Child2.prototype是Child2构造函数的一个属性,这个时候prototype被赋值了parent2的一个实例,实例化了新的对象Child2()的时候, 会有一个__proto__属性,这个属性就等于起构造函数的原型对象,但是原型对象被赋值为了parent2的一个实例, 所以new Child2的原型链就会一直向上找parent2的原型 var s1=new Child2(); var s2=new Child2(); s1.__proto__===s2.__proto__;//返回true 缺点:通过子类构造函数实例化了两个对象,当一个实例对象改变其构造函数的属性的时候, 那么另外一个实例对象上的属性也会跟着改变(期望的是两个对象是隔离的赛);原因是构造函数的原型对象是公用的; # 3、组合方式 ```javascript /*组合方式*/ function Parent3(){ this.name='parent3'; this.arr=[1,2,3]; } function Child3(){ Parent3.call(this); this.type='child'; } Child3.prototype=new Parent3(); var s3=new Child3(); var s4=new Child3(); s3.arr.push(4); console.log(s3,s4); ``` **优点:**这是最通用的使用方法,集合了上面构造函数继承,原型链继承两种的优点。 **缺点:**父类的构造函数执行了2次,这是没有必要的, constructor指向了parent了 # 4、组合继承的优化 ```javascript /*组合继承的优化1*/ function Parent4(){ this.name='parent3'; this.arr=[1,2,3]; } function Child4(){ Parent4.call(this); this.type='child5'; } Child4.prototype=Parent4.prototype; var s5=new Child4(); var s6=new Child4() ``` **缺点:**s5 instaceof child4 //true, s5 instanceof Parent4//true 我们无法区分一个实例对象是由其构造函数实例化,还是又其构造函数的父类实例化的 s5.constructor 指向的是Parent4;//原因是子类原型对象的constructor 被赋值为了父类原型对象的 constructor,所以我们使用constructor的时候,肯定是指向父类的 Child3.constructor 也有这种情况 # 5、组合继承的优化2 ```javascript function Parent5() { this.name = 'parent5'; this.play = [1, 2, 3]; } function Child5() { Parent5.call(this); this.type = 'child5' } Child5.prototype = Object.create(Parent5.prototype); //这个时候虽然隔离了,但是constructor还是只想的Parent5的,因为constructor会一直向上找 Child5.prototype.constructor=Child5; var s7=new Child5(); console.log(s7 instanceof Child5,s7 instanceof Parent5); console.log(s7.constructor); ``` 通过Object.create来创建原型中间对象,那么这么来的话,chiild5的对象prototype获得的是parent5 父类的原型对象; Object.create创建的对象,原型对象就是参数; # 6、ES 中的继承 Class 可以通过extends关键字实现继承,让子类继承父类的属性和方法。extends 的写法比 ES5 的原型链继承,要清晰和方便很多。 ```js class Point { /* ... */ } class ColorPoint extends Point { constructor(x, y, color) { super(x, y); // 调用父类的constructor(x, y) this.color = color; } toString() { return this.color + ' ' + super.toString(); // 调用父类的toString() } } ```
# 1、借助构造函数实现继承 call和apply改变的是JS运行的上下文: ```javascript /*借助构造函数实现继承*/ function Parent(name) { this.name = name; this.getName = function () { console.log(this.name); } } function Child(name) { Parent.call(this, name); this.type = 'child1' } let child = new Child('yanle'); child.getName(); console.log(child.type); ``` 父类的this指向到了子类上面去,改变了实例化的this 指向,导致了父类执行的属性和方法,都会挂在到 子类实例上去; 缺点:父类原型链上的东西并没有被继承; # 2、通过原型链实现继承 ```javascript /*通过原型链实现继承*/ function Parent2(){ this.name='parent2' } function Child2(){ this.type='child2' } Child2.prototype=new Parent2(); console.log(new Child2()); ``` Child2.prototype是Child2构造函数的一个属性,这个时候prototype被赋值了parent2的一个实例,实例化了新的对象Child2()的时候, 会有一个__proto__属性,这个属性就等于起构造函数的原型对象,但是原型对象被赋值为了parent2的一个实例, 所以new Child2的原型链就会一直向上找parent2的原型 var s1=new Child2(); var s2=new Child2(); s1.__proto__===s2.__proto__;//返回true 缺点:通过子类构造函数实例化了两个对象,当一个实例对象改变其构造函数的属性的时候, 那么另外一个实例对象上的属性也会跟着改变(期望的是两个对象是隔离的赛);原因是构造函数的原型对象是公用的; # 3、组合方式 ```javascript /*组合方式*/ function Parent3(){ this.name='parent3'; this.arr=[1,2,3]; } function Child3(){ Parent3.call(this); this.type='child'; } Child3.prototype=new Parent3(); var s3=new Child3(); var s4=new Child3(); s3.arr.push(4); console.log(s3,s4); ``` **优点:**这是最通用的使用方法,集合了上面构造函数继承,原型链继承两种的优点。 **缺点:**父类的构造函数执行了2次,这是没有必要的, constructor指向了parent了 # 4、组合继承的优化 ```javascript /*组合继承的优化1*/ function Parent4(){ this.name='parent3'; this.arr=[1,2,3]; } function Child4(){ Parent4.call(this); this.type='child5'; } Child4.prototype=Parent4.prototype; var s5=new Child4(); var s6=new Child4() ``` **缺点:**s5 instaceof child4 //true, s5 instanceof Parent4//true 我们无法区分一个实例对象是由其构造函数实例化,还是又其构造函数的父类实例化的 s5.constructor 指向的是Parent4;//原因是子类原型对象的constructor 被赋值为了父类原型对象的 constructor,所以我们使用constructor的时候,肯定是指向父类的 Child3.constructor 也有这种情况 # 5、组合继承的优化2 ```javascript function Parent5() { this.name = 'parent5'; this.play = [1, 2, 3]; } function Child5() { Parent5.call(this); this.type = 'child5' } Child5.prototype = Object.create(Parent5.prototype); //这个时候虽然隔离了,但是constructor还是只想的Parent5的,因为constructor会一直向上找 Child5.prototype.constructor=Child5; var s7=new Child5(); console.log(s7 instanceof Child5,s7 instanceof Parent5); console.log(s7.constructor); ``` 通过Object.create来创建原型中间对象,那么这么来的话,chiild5的对象prototype获得的是parent5 父类的原型对象; Object.create创建的对象,原型对象就是参数; # 6、ES 中的继承 Class 可以通过extends关键字实现继承,让子类继承父类的属性和方法。extends 的写法比 ES5 的原型链继承,要清晰和方便很多。 ```js class Point { /* ... */ } class ColorPoint extends Point { constructor(x, y, color) { super(x, y); // 调用父类的constructor(x, y) this.color = color; } toString() { return this.color + ' ' + super.toString(); // 调用父类的toString() } } ```
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
JavaScript
未设置
标签管理
里程碑
初
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (1)
标签 (64)
master
0.0.76
0.0.75
0.0.74
0.0.73
0.0.72
0.0.71
0.0.70
0.0.69
0.0.68
0.0.67
0.0.66
0.0.65
0.0.64
0.0.63
0.0.62
0.0.61
0.0.60
0.0.59
0.0.58
0.0.57
0.0.56
0.0.55
0.0.54
0.0.53
0.0.52
0.0.51
0.0.50
0.0.49
0.0.48
0.0.47
0.0.46
0.0.45
0.0.44
0.0.43
0.0.42
0.0.41
0.0.40
0.0.39
0.0.38
0.0.37
0.0.36
0.0.35
0.0.34
0.0.33
0.0.32
0.0.31
0.0.30
0.0.29
0.0.28
0.0.27
0.0.26
0.0.25
0.0.24
0.0.23
0.0.22
0.0.21
0.0.20
0.0.19
0.0.18
0.0.17
0.0.16
0.0.15
0.0.14
0.0.13
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
TypeScript
1
https://gitee.com/yanleweb/interview-question.git
git@gitee.com:yanleweb/interview-question.git
yanleweb
interview-question
interview-question
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册