浅析5种Javascript继承关系的应用 - Web前端
作者:98wpeu发布时间:2026-09-16分类:网页前端技术浏览:2
导读:在Javascript中如何实现继承关系的应用?Javascript继承概念:JS是基于对象的,他没有类的概念,所以实现继承,需要使用js的原型prototype机制或者用APPl...

在Javascript中如何实现继承关系的应用?Javascript继承概念:JS是基于对象的,他没有类的概念,所以实现继承,需要使用js的原型prototype机制或者用APPlay和call方法实现。
1、原型链继承:
即子类通过PRototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承
为了让子类继承父类的属性(也包括方法),首先需要定义一个构造函数。然后,将父类的新实例赋值给构造函数的原型。
function parent(){
this.name="garuda";
}
function child(){
this.sex="man"
}
child.prototype=new parent();//核心:子类继承父类,通过原型形成链条
var test=new child();
console.log(test.name);
console.log(test.sex);备注:在js中,被继承的函数称为超类型(父类、基类),继承的函数称为子类型(子类、派生类)。
使用原型继承存在两个问题:一是面量重写原型会中断关系,使用引用类型的原型,二是子类型还无法给超类型传递参数
2、借用构造函数继承
function parent(){
this.name="garuda";
}
function child(){
parent.call(this);//核心:借父类型构造函数增强子类型(传参)
}
var test =new parent();
console.log(test.name);3、call()方法方式
call方法是Function类中的方法
call方法的第一个参数的值赋值给类(即方法)中出现的this
call方法的第二个参数开始依次赋值给类(即方法)所接受的参数
function test(str){
alert(this.name + " " + str);
}
var Object = new Object();
object.name = "zhangsan";
test.call(object,"langsin");//此时,第一个参数值object传递给了test类(即方法)中出现的this,而第二个参数"langsin"则赋值给了test类(即方法)的str
function Parent(Username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.call(this,username);
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();4、apply()方法方式
apply方法接受2个参数,
A、第一个参数与call方法的第一个参数一样,即赋值给类(即方法)中出现的this
B、第二个参数为数组类型,这个数组中的每个元素依次赋值给类(即方法)所接受的参数
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.Apply(this,new Array(username));
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi","123456");
parent.hello();
child.hello();
child.world();5、组合继承(原型链和构造函数组合)
function parent(){
this.name="garuda";
}
function borther(){
return this.name;
}
function child(){
parent.call(this)
}
child.prototype=new parent();
var test=new parent();
console.log(test.borther())总结
JS中的继承关系是很重要的技术知识了,经常会用到,不了解的童鞋需要加紧学习哦!
相关推荐
- 关于xshell连接不上阿里云服务器【提示:Xshell Connection established】 - Web前端
- 说好的基于chrome内核的全新win10Edge正式版来啦 - Web前端
- 用于Chrome/Chromium的基于节点的库:Puppeteer - Web前端
- Web前端开发人员常用的5款编辑器 - Web前端
- 面向设计师的一些最佳思维导图工具 - Web前端
- XMLHttpRequest和Fetch API,您认为哪种最适合Ajax? - Web前端
- Google Analytics(分析)如何协助解决UX问题 - Web前端
- Web前端开发中SVG在CSS里的玩法,您学会了吗? - Web前端
- 汇总IOS下奇葩BUG以及iframe嵌套页面带来的一些困扰 - Web前端
- 在JavaScript中,将存储布尔值的变量转换为“0”或“1”的常用方法 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery编写widget的一些技巧分享 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 9在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


