javascript如何实现继承?有哪些继承方法?
Admin 2021-05-17 群英技术资讯 1006 次浏览
javascript如何实现继承?有哪些继承方法?很多刚接触javascrip继承的朋友,可能对于javascrip继承方法不是很清楚,因此这篇文章文章就给大家介绍一下JavaScript 实现继承的几种方式,小编认为是比较实用的,下面就跟随小编一起来了解一下吧。
实现
function Super(age){ this.age = age; this.say = function(){ console.log(this.age) } } function Child(name,age){ Super.call(this,age) this.name = name; } var child = new Child("min",23) console.log(child instanceof Super); // false console.log(child instanceof Child); // true
优点
(1) 可以实现多继承(call多个父类对象)
(2) 构造函数中可向父级传递参数
缺点
(1) 只能继承父类实例的属性和方法,不能继承原型上的属性和方法
(2) 实例并不是父类的实例,只是子类的实例
实现
function Super(){ this.getName = function(){ console.log(this.name) } } function Child(name){ this.name = name; } Child.prototype = new Super(); // 这里可以传构造参数 Child.prototype.constructor = Child; var child = new Child("min"); console.log(child instanceof Super); // true console.log(child instanceof Child); // true console.log(child.constructor); // Child
优点
(1) 父类原型属性与方法,子类都能访问到
(2) 实例是子类的实例,也是父类的实例
缺点
(1) 无法实现多继承 (2) 创建子类实例时,无法向父类构造函数传参
实现
function Super(age){ this.age = age; this.getAge = function(){ console.log(this.age); } } function Child(name,age){ Super.call(this,age) this.name = name; } Child.prototype = new Super(1); Child.prototype.constructor = Child; var child = new Child("min",23); console.log(child instanceof Super); // true console.log(child instanceof Child); // true console.log(child.constructor); // Child
优点
(1) 结合了构造+原型链继承的优点
缺点
(1) Child.prototype = new Super(); 多调用了一次,使得原型对象中存在一些不必要属性,如上面例子中age属性
实现
function Super(age){ this.age = age; this.getAge = function(){ console.log(this.age) } } function Child(name,age){ Super.call(this,age) this.name = name; } (function(){ function Copy(){} Copy.prototype = Super.prototype; Child.prototype = new Copy(); })() Child.prototype.constructor = Child; var child = new Child("min",23);
备注
问:为什么没有直接使用 Child.prototype = Super.prototype;
答:Child.prototype.constructor = Child;关键代码,上面写Super.prototype 也会变(引用类型,指向同一地址)
优点
(1) 这应该是实现继承最完美的方案了,es6的extends关键字,在babel转换后代码也是通过这种方式实现的继承。
实现
function Super(age){ this.age = age; this.getAge = function(){ console.log(this.age) } } function Child(name,age){ Super.call(this,age) this.name = name; } Child.prototype = Object.create(Super.prototype,{ constructor:{ // 构造函数修复 value: Child } }) var child = new Child("min",23); console.log(child instanceof Super); // true console.log(child instanceof Child); // true console.log(child.constructor); // Child
关于javascript如何实现继承的介绍就到这,希望对大家学习有一定的帮助,想要了解更多javascript继承的内容大家可以继续关注其他文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Vue实现PC端靠边悬浮球的代码,我想把退出登录的按钮做成一个悬浮球的样子,带动画的那种。
这篇文章主要给大家介绍关于JavaScript原型与原型链的内容,一些朋友可能对JavaScript原型和JavaScript原型链是什么不是很了解,对此本文有很详细的介绍,需要的朋友了解看看,那么接下来就跟随小编来学习一下吧。
今天主要给大家分享用JS怎样学习算法复杂度的内容,一些朋友可能对于算法复杂度不是很了解,对此本文就给大家来介绍一下算法复杂度,下文代码有一定的参考价值,需要的朋友可以看一看。
用React怎样做导入导出Excel的功能?在实际的项目中,一些需要需要我们做导入导出Excel的功能,那么具体该如何实现呢?接下来我们直接看用React实现导入导出Excel的代码,感兴趣的朋友可以参考。
tab栏的实现相信大家都比较熟悉了,这篇文章给大家分享的是用JavaScript实现tab栏的方法,本文有详细的思路及代码介绍,对新手学习用JavaScript实现tab栏有一定的帮助,需要的朋友可以参考。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008