javascript如何实现继承?有哪些继承方法?
Admin 2021-05-17 群英技术资讯 1496 次浏览
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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了一个处理react异常的ErrorBoundary组件,简单实用,代码详细,对这个组件感兴趣的朋友可以参考下
Vue众所周知是一个轻量级的框架,源码仅仅为72.9KB,但是也有它自己的缺点,就是首屏加载会比较慢,这篇文章主要给大家介绍了关于Vue项目首屏性能优化组件的相关资料,需要的朋友可以参考下
jQuery如何实现鼠标拖动图片?鼠标拖动图片就与鼠标拖动桌面软件图标差不多,也就是图片的位置随着鼠标的移动位置而改变。这篇文章就通过详细的实例代码给大家介绍一下jQuery如何实现鼠标拖动图片的功能。
使用js删除javascript对象的某个属性的方法有3种,我们使用的了js的delete函数,下面是我对js删除对象属性的方法总结,是本人在项目中经过测试的。 js代码 script//定义一个对象varmyObject={username:zhangsan,password:123456,address:hubei};//方式一//de
JavaScript中判断奇偶数的方法是什么?在JavaScript中,求奇偶数的方法有很多,这里主要是使用if语句来实现,对新手学习if语句的使用会有一定的帮助,文中示例代码介绍的很详细,感兴趣的朋友接下来一起跟随小编看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008