博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript继承总结
阅读量:7040 次
发布时间:2019-06-28

本文共 2429 字,大约阅读时间需要 8 分钟。

1.创建对象

1.字面量对象 2.构造函数 3.Object.create

//1.字面量var obj={    name: '字面量',    show: function(){        console.log(this.name)    }}//2.构造函数function fun (name) {    this.name=name}var obj=new fun('obj')//3.Object.createvar obj={name: 'obj'}var obj=Object.create(obj)复制代码

2.JavaScript继承

1.原型链继承

function Parent(name){	this.name=name	this.sleep=function(){		console.log(this.name + '在睡觉')	}}Parent.prototype.eat=function(food){	console.log(this.name + '正在吃' + food)}function Child(){}Child.prototype=new Parent('Child')Child.prototype.constructor=Childvar child=new Child()复制代码

Child.prototype=new Parent('Child')就是把Parent实例赋值给Child.prototype,也就是说new Child().__proto__===new Parent('Child')

可以通过Child.prototype在原型对象上增加新的属性或方法,也可以通过,child.__proto__在原型对象上添加新的方法和属性。

缺点: 1.原型对象上的属性和方法是所有实例都可访问的,而且一个实例改变了原型上的方法和属性都会影响到其他实例。 2.创建子类实例时,无法向父类构造函数传参。

2.构造函数继承

function Parent(name){	this.name=name	this.sleep=function(){			console.log(this.name + '在睡觉')	}}Parent.prototype.eat=function(food){	console.log(this.name + '正在吃' + food)}function Child(){	Parent.call(this,'child')}Child.prototype.eyes=function(){console.log('eyes')}var child=new Child()复制代码

构造函数继承可以通过call或apply方法实现继承。这种方法不能继承原型对象中的属性和方法,只能继承实例属性和实例方法,但是可以向父类传参。

3.组合继承

function Parent(name){	this.name=name	this.sleep=function(){			console.log(this.name + '正在睡觉')	}}Parent.prototype.eat=function(food){	console.log(this.name + '正在吃' + food)}function Child(){	Parent.call(this,'child')}Child.prototype.eyes=function(){console.log('eyes')}Child.prototype=new Parent()Child.prototype.constructor=Child var child=new Child()复制代码

组合继承是比较好的继承, 他是原型链继承和构造函数继承的结合, 合理的利用了这两种组合的特点,既是子类的实例,也是父类的实例, 但有一个缺点就是调用了两次构造函数。

4.组合继承优化

function Parent(name){	this.name=name	this.sleep=function(){			console.log(this.name + '正在睡觉')	}}Parent.prototype.eat=function(food){	console.log(this.name + '正在吃' + food)}function Child(){	Parent.call(this,'child')}Child.prototype = Object.create(Parent.prototype)Child.prototype.constructor=Child var child=new Child()复制代码

5.寄生组合继承

function Parent(name){	this.name=name	this.sleep=function(){			console.log(this.name + '正在睡觉')	}}Parent.prototype.eat=function(food){	console.log(this.name + '正在吃' + food)}function Child(){	Parent.call(this,'child')}function f(){}f.prototype=Parent.prototypeChild.prototype=new f()Child.prototype.constructor=Child var child=new Child()复制代码

只调用一次父类的构造函数,避免了在子类原型上创建不必要的,多余的属性。

转载于:https://juejin.im/post/5b8becaee51d4556f30b7b3e

你可能感兴趣的文章
架构师速成4.8-幼儿园书单资料推荐
查看>>
MySQL-Proxy实现读写分离部署文档
查看>>
For Update
查看>>
Hyper-V 之03 创建iSCSI存储和故障转移群集
查看>>
如何成为一名架构师?
查看>>
我的友情链接
查看>>
nfs failed, reason given by server: Permission denied的离奇解决
查看>>
2018 1.21测试
查看>>
DFS与BFS对比
查看>>
dedeCMS php语法在模版中的应用
查看>>
sublime 安装ctag 实现函数跳转
查看>>
sshd问题:A protocol error occurred. Change of username or service not allowed
查看>>
jQuery开发者眼中的AngularJS
查看>>
【DAY9】 关于多线程熊吃蜜Demo1的作业实验
查看>>
Python实现多属性排序
查看>>
nginx 访问日志分析
查看>>
RabbitMQ之消息确认机制(事务+Confirm)
查看>>
给出一个数组,计算数组中少了哪个数据的实现
查看>>
USB-232卡 配置
查看>>
C#窗体程序皮肤设置
查看>>