javascript如何实现继承?有哪些继承方法?
Admin 2021-05-17 群英技术资讯 1375 次浏览
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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是jQuery怎样对文本框字数限制的方法。一般的文本输入框都是有字数限制的,下文我们尝试用jQuery怎样计算输入字数及控制和限制字数,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
这篇文章主要为大家详细介绍了原生JavaScript实现贪吃蛇游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
作用域是指程序源代码中定义变量的区域,作用域规定了如何查找变量,也就是确定当前执行代码对变量的访问权限,这篇文章主要给大家介绍了关于JavaScript静态作用域和动态作用域的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Vue组件复用和扩展,对vue感兴趣的同学,可以参考下
这篇文章给大家分享的是一些JavaScript优化技巧。小编觉得挺实用的,因此分享给大家做个参考,文中的示例代码介绍得很详细,对大家学习或工作都有一定的帮助,接下来跟随小编一起了解看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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备09006778号 域名注册商资质 粤 D3.1-20240008