JS中的继承方式有哪些?
1. 原型链继承
Child.prototype = new Parent();
1 function Parent (name, age) { 2 this.name = name; 3 this.age = age; 4 } 5 Parent.prototype.say = function(){ 6 console.log('hello, my name is ' + this.name); 7 }; 8 function Child() { 9 }10 Child.prototype = new Parent('pursue');11 var child1 = new Child();12 child1.say(); //hello, my name is pursue13 var child2 = new Child();14 console.log(child1.say === child2.say);//true15 console.log(child1.name === child2.name);//true
2. call(thisObj, param1, param2,...)
1 function Parent(username){ 2 this.username = username; 3 this.hello = function(){ 4 alert(this.username); 5 } 6 } 7 function Child(username,password){ 8 Parent.call(this,username); 9 this.password = password;10 this.world = function(){11 alert(this.password);12 }13 }14 var parent = new Parent("zhangsan");15 var child = new Child("lisi","123456");16 parent.hello();17 child.hello();18 child.world();
3. apply(thisObj, [param1,param2,...])
1 function Parent(username){ 2 this.username = username; 3 this.hello = function(){ 4 alert(this.username); 5 } 6 } 7 function Child(username,password){ 8 Parent.apply(this,new Array(username)); 9 this.password = password;10 this.world = function(){11 alert(this.password);12 }13 }14 var parent = new Parent("zhangsan");15 var child = new Child("lisi","123456");16 parent.hello();17 child.hello();18 child.world();
4. 组合继承(call+原型链 / apply+原型链)
1 function Parent(hello){ 2 this.hello = hello; 3 } 4 Parent.prototype.sayHello = function(){ 5 alert(this.hello); 6 } 7 function Child(hello,world){ 8 Parent.call(this,hello);//利用 call 方法 将父类的属性继承过来 9 //Parent.apply(this,new Array(hello));//利用 apply 方法 将父类的属性继承过来10 this.world = world;//新增一些属性11 }12 Child.prototype = new Parent();//将父类的方法继承过来13 Child.prototype.sayWorld = function(){//新增一些方法14 alert(this.world);15 }16 var c = new Child("zhangsan","lisi");17 c.sayHello();18 c.sayWorld();
5.寄生组合继承,与4相似,只是将原型链换做了Object.create(Parent.prototype)
1 function Parent(hello){ 2 this.hello = hello; 3 } 4 Parent.prototype.sayHello = function(){ 5 alert(this.hello); 6 } 7 function Child(hello,world){ 8 Parent.call(this,hello);//利用 call 方法 将父类的属性继承过来 9 //Parent.apply(this,new Array(hello));//利用 apply 方法 将父类的属性继承过来10 this.world = world;//新增一些属性11 }12 Child.prototype = Object.create(Parent.prototype);//将父类的方法继承过来13 Child.prototype.sayWorld = function(){//新增一些方法14 alert(this.world);15 }16 var c = new Child("zhangsan","lisi");17 c.sayHello();18 c.sayWorld();
以上是JS中的继承方式有哪些?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

如何使用WebSocket和JavaScript实现在线语音识别系统引言:随着科技的不断发展,语音识别技术已经成为了人工智能领域的重要组成部分。而基于WebSocket和JavaScript实现的在线语音识别系统,具备了低延迟、实时性和跨平台的特点,成为了一种被广泛应用的解决方案。本文将介绍如何使用WebSocket和JavaScript来实现在线语音识别系

WebSocket与JavaScript:实现实时监控系统的关键技术引言:随着互联网技术的快速发展,实时监控系统在各个领域中得到了广泛的应用。而实现实时监控的关键技术之一就是WebSocket与JavaScript的结合使用。本文将介绍WebSocket与JavaScript在实时监控系统中的应用,并给出代码示例,详细解释其实现原理。一、WebSocket技

Linux下system()函数的总结在Linux系统中,system()函数是一个非常常用的函数,它可以用于执行命令行命令。本文将对system()函数进行详细的介绍,并提供一些具体的代码示例。一、system()函数的基本用法system()函数的声明如下:intsystem(constchar*command);其中,command参数是一个字符

如何利用JavaScript和WebSocket实现实时在线点餐系统介绍:随着互联网的普及和技术的进步,越来越多的餐厅开始提供在线点餐服务。为了实现实时在线点餐系统,我们可以利用JavaScript和WebSocket技术。WebSocket是一种基于TCP协议的全双工通信协议,可以实现客户端与服务器的实时双向通信。在实时在线点餐系统中,当用户选择菜品并下单

如何使用WebSocket和JavaScript实现在线预约系统在当今数字化的时代,越来越多的业务和服务都需要提供在线预约功能。而实现一个高效、实时的在线预约系统是至关重要的。本文将介绍如何使用WebSocket和JavaScript来实现一个在线预约系统,并提供具体的代码示例。一、什么是WebSocketWebSocket是一种在单个TCP连接上进行全双工

JavaScript和WebSocket:打造高效的实时天气预报系统引言:如今,天气预报的准确性对于日常生活以及决策制定具有重要意义。随着技术的发展,我们可以通过实时获取天气数据来提供更准确可靠的天气预报。在本文中,我们将学习如何使用JavaScript和WebSocket技术,来构建一个高效的实时天气预报系统。本文将通过具体的代码示例来展示实现的过程。We

JavaScript教程:如何获取HTTP状态码,需要具体代码示例前言:在Web开发中,经常会涉及到与服务器进行数据交互的场景。在与服务器进行通信时,我们经常需要获取返回的HTTP状态码来判断操作是否成功,根据不同的状态码来进行相应的处理。本篇文章将教你如何使用JavaScript获取HTTP状态码,并提供一些实用的代码示例。使用XMLHttpRequest

用法:在JavaScript中,insertBefore()方法用于在DOM树中插入一个新的节点。这个方法需要两个参数:要插入的新节点和参考节点(即新节点将要被插入的位置的节点)。
