javascript如何实现继承?有哪些继承方法?
Admin 2021-05-17 群英技术资讯 923 次浏览
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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
最近产品经理提出了很多用户体验优化的需求,涉及到很多dom的操作。小张:“老铁,本来开发Vue2项目操作dom挺简单的,现在开发vue3项目,突然感觉一头雾水!”我:“没事,
本文给大家分享用vue怎样做拖拽排序的功能,小编觉得比较实用,因此分享给大家做个参考,实现效果及代码如下,这里提供一种简单的实现方案,感兴趣的朋友可以了解看看。
首先到nodejs的官网安装nodejs,地址nodejs.org,网站第一页会根据你的电脑系统推荐你适合的版本,下载,不断next,在cmd中输入node-v可以看到版本的话,即安装成功。说到js,大家一定头痛的就是导入众多js代码,不可避免的函数冲突问题,nodejs的一个好处就是各个函数之间相互独立,不会冲突。nodejs:url.parseurl.formaturl.res
vue里route和router的区别在哪?新手在学习vue框架是,很容易将$route和$router混淆,对此这篇文章就给大家介绍一下$route和$router的区别是什么,感兴趣的朋友就往下看吧。
查询方法:1、点击“开始”按钮,打开“开始”菜单;2、在搜索框中输入“cmd”,点击“cmd.exe”程序来打开cmd命令窗口;3、在打开的cmd命令行中,使用cd命令进入nodejs安装目录,执行“node -v”命令即可查看到版本信息。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008