js实现继承的各种方法及缺点

2024-05-13 20:48
文章标签 实现 方法 js 继承 缺点

本文主要是介绍js实现继承的各种方法及缺点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本篇文章主要介绍了js中继承的几种用法总结。

1.构造函数实现继承

代码:

  function Parent(username){this.username = username;this.hello = function(){alert(this.username);}}Parent.prototype.bye = function(){alert(this.username+": Goodbye!");};function Child(username,password){this.father = Parent;this.father(username);//最关键的一行delete this.father;
//以上3行等价于Parent.call(this,username)或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();//zhangsanchild.hello();//lisichild.world();//123456parent.bye(); //zhangsan :Goodbyechild.bye();// Error: child.bye is not a function

缺点:由于方法在构造函数内创建,不能做到函数复用而降低效率,并且不能继承父类的prototype定义的属性与方法。

2.原型链方式:子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承

代码:

function Parent(){this.elements=["ul","li","p"];}Parent.prototype.hello = "hello";Parent.prototype.sayHello = function(){alert(this.hello);}function Child(){}Child.prototype = new Parent();//这行的作用是:将Parent中将所有通过prototype追加的属性和方法都追加到Child,从而实现了继承Child.prototype.world = "world";Child.prototype.sayWorld = function(){alert(this.world);}var c = new Child();c.sayHello();//helloc.sayWorld();//worldalert(c.elements);//ul,li,pc.elements.push("div");c.hello="div";var c2 = new Child();c2.sayHello();//helloalert(c2.elements);//ul,li,p,div


缺点:由于prototype是各个子类的实例共享的,故对某一个子类prototype中的属性进行修改,也会体现在所有子类上(如以上代码,Child.prototype 继承了Parent的实例,故Child.prototype.elements为所有Child的实例共享),另外本方法还有一个缺点是不能想超类的构造函数传参数。
3.组合继承:结合以上两种方式,用prototype继承超类的prototype的属性、方法,用构造函数继承超类的实例的属性、方法。
代码:
 function Parent(username){this.elements =["li","ul","p"];this.username = username;this.hello = function(){alert(this.username);}}Parent.prototype.hello = "hello";Parent.prototype.bye = function(){alert(this.username+": Goodbye!");};function Child(username,password){Parent.call(this,[username]);this.password = password;this.word = function(){alert(this.password);}}Child.prototype=new Parent();var parent = new Parent("zhangsan");var child = new Child("lisi","123456");parent.hello();//zhangsanchild.hello();//lisichild.word();//123456child.elements.push("div");var child2 = new Child("wangwu","111111");alert(child2.elements);//ul,li,p


缺点:与构造函数模式相比,函数复用问题未解决。
4.寄生式继承:寄生式继承是创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再返回对象。
 var person = {name : "Nicholas", friends :["shelby","Court","Van"],
sayGoodbye :function(){alert("Goodbye");}};function createSon(original){  var clone = original;//通过调用函数创建一个新对象  clone.sayHi =function(){ //以某种方式来增强这个对象  alert("hi");};  return clone;//返回这个对象  }
var son = createSon(person);  
son.sayHi();//"hi"  
son.sayGoodbye();


缺点:与构造函数模式类似,不能做到函数复用而降低效率。


5.寄生组合式继承:本质上,就是使用寄生式继承来继承超类型的prototype,然后再将结果指定给子类型的prototype。

代码:

function inheritPrototype(subType, superType) {var prototype = superType.prototype; prototype.constructor = subType; subType.prototype = prototype; 
}function Parent(name) {this.name = name;this.elements=["ul","li","p"];}Parent.prototype.sayName = function() {alert(this.name);}function Child(name, age) {Parent.call(this, name);this.age = age;}inheritPrototype(Child, Parent);Child.prototype.sayAge = function() {alert(this.age);}var parent = new Parent("zhangsan");var child = new Child("lisi","12");parent.sayName ();//zhangsanchild.sayName ();//lisichild.sayAge();//12child.elements.push("div");var child2 = new Child("wangwu","22");child2.sayAge();//22alert(child2.elements);//li,ul,p



这篇关于js实现继承的各种方法及缺点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/986786

相关文章

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

PyQt6/PySide6中QTableView类的实现

《PyQt6/PySide6中QTableView类的实现》本文主要介绍了PyQt6/PySide6中QTableView类的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录1. 基本概念2. 创建 QTableView 实例3. QTableView 的常用属性和方法

PyQt6/PySide6中QTreeView类的实现

《PyQt6/PySide6中QTreeView类的实现》QTreeView是PyQt6或PySide6库中用于显示分层数据的控件,本文主要介绍了PyQt6/PySide6中QTreeView类的实现... 目录1. 基本概念2. 创建 QTreeView 实例3. QTreeView 的常用属性和方法属性

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE