继承的几种方式

1.借助构造函数实现继承

function Parent() {
this.name = 'parent'
}
Parent.prototype.say = function () { // 不能被继承
this.say = function() {
console.log('hello'+ this.name)
}
}
function Child() {
Parent.call(this)
this.type = 'child'
}
console.log(new Child) // 没有参数可以不写最后的()

call方法改变了函数运行的上下文(this的指向,指向了Child实例化的对象引用),将父级构造函数的this指向子类构造函数的实例上去。执行时父类的方法和属性都挂载到Child类的实例上

缺点:
父类的原型对象上有属性和方法不能被子类继承

 

2.借助原型链实现继承

function Parent2() {
this.name = 'parent2'
this.list = [1,2,5]
}
Parent2.prototype.say = function () { // 不能被继承
this.say = function() {
console.log('hello'+ this.name)
}
}
function Child2() {
this.type = 'child'
}
Child2.prototype = new Parent2()
var c21 = new Child2()
var c22 = new Child2()// 缺点
c21.list.push(6)
console.log(c22.list) // [1, 2, 5, 6]

prototype就是让Child的实例可以访问到原型链上

根据原型链
由 Child2.prototype = new Parent2()
和 c21.__proto__ === Child2.prototype
得 c21.__proto__ 指向了new Parent2()
所以 c21.__proto__.__proto__ 与 new Parent2().__proto__为同一个对象
又因为 new Parent2().__proto__ === Parent2.prototype
所以c21.__proto__.__proto__指向 Parent2.prototype

缺点:
原型链中的原型对象是公用的

 

3.组合继承

function Parent3() {
this.name = 'parent3'
this.list = [1,2,5]
}
function Child3() {
Parent3.call(this)
this.type = 'child3'
}
Child3.prototype = new Parent3()
var c3 = new Child3()

缺点:
Parent3()执行了两次,一次在Child3()里面,一次是给Child2.prototype赋值时

 

 

4. 组合继承优化1

function Parent4() {
this.name = 'parent4'
this.list = [1,2,5]
}
function Child4() {
Parent5.call(this)
this.type = 'child4'
}
Child4.prototype = Parent4.prototype
var c4 = new Child4()// 缺点
console.log(c31.__proto__.constructor) // Parent4

缺点:
因为Child4.prototype = Parent4.prototype,所以Child4没有构造器,是从父类继承的

 

 

5. 组合继承优化2

function Parent5() {
this.name = 'parent5'
this.list = [1,2,5]
}
function Child5() {
Parent5.call(this)
this.type = 'child5'
}
Child5.prototype = Object.create(Parent5.prototype)
Child5.prototype.constructor = Child5 
var c5 = new Child5()

缺点:
。。。可能就是比较麻烦吧

 

6.ES6实现继承(与5的效果相同)

class Animal {constructor(name='animal') {this.name = name}eat () {console.log('I can eat')}
}class Dog extends Animal {constructor(name='dog') {super(name)}bark() {console.log('bark')}
}let dog1 = new Dog 
let dog2 = new Dog('dd')

 

转载于:https://www.cnblogs.com/daisy-ramble/p/10331392.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278385.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

写一个简单的 django_post demo

1.新建一个django工程,其路由为下图 2.要做的是一个 简单的登录请求,以表单形式提交,html 部分代码如下 这里注意action指向的是路由的地址,index1后的views.login部分代码如下 这段代码指的是,如果login接收到的请求是…

日志收集

2019独角兽企业重金招聘Python工程师标准>>> ELK (ElasticSearch、Logstash、Kibana): https://my.oschina.net/itblog/blog/547250 转载于:https://my.oschina.net/zfscofield/blog/1625703

autocopy2u_借助AutoCopy简化Firefox中的文本复制和粘贴

autocopy2uLooking for an easy way to speed up copying and pasting in Firefox? Now you can reduce the amount of work that you have to do by half with AutoCopy. 是否在寻找一种简便的方法来加快Firefox中的复制和粘贴? 现在,您可以使用自动复…

virtualenv模块使用

开发多个应用: 如A需要jinja2.7开发;如B需要jinja2.6开发。或者C需要Python2.7开发,D需要Python3.5开发 那么解决上述问题就需要使用virtualenv这个模块: 它的作用是:创建“隔离”环境,使项目拥有独立的Pyt…

僵尸进程处理方式

Linux服务器上,多少会出现一些僵尸进程,下面介绍如何快速寻找和消灭这些僵尸进程的方法 首先,我们可以用top命令来查看服务器当前是否有僵尸进程,在下图中可以看到僵尸进程数的提示,如果数字大于0,那么意味…

chromebook刷机_如何查看Chromebook的停产日期

chromebook刷机Google谷歌There comes a time in your Chromebook’s life when it no longer receives updates from Google. It’s inevitable and could be a lot sooner than you think. Here’s how to see your Chromebook’s scheduled end-of-life date. Chromebook一生…

C#将unix时间戳转换成.net的DateTime类型的代码

下面的内容是关于C#将unix时间戳转换成.net的DateTime类型的内容。 DateTime epoch new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);DateTime myDate epoch.AddSeconds(1258598728).toLocalTime(); 转载于:https://www.cnblogs.com/odsxe/p/10338494.html

【活动】AI人工智能技术沙龙 |杭州站

AI人工智能技术沙龙 |杭州站将于2018年3月3号在浙江杭州市文一西路1818-2号中国(杭州)人工智能小镇举办由袋鼠云、七牛云及“因特链”社区的老师为大家带来AI纯技术干货分享另有区块链和AI人工智能技术融合技术主题1活动安排时间:2018年3月3号…

如何在Google文档中的图片周围换行

If you want to insert an image or object into a document, it’s relatively simple. However, positioning and getting them to stay where you want can be frustrating. The wrap text feature in Google Docs makes all of this more manageable. 如果要将图像或对象插…

mysql的left函数

1、LEFT()函数是一个字符串函数,它返回具有指定长度的字符串的左边部分。 LEFT(Str,length); 接收两个参数: str:一个字符串; length:想要截取的长度,是一个正整数; 2、示例: SELECT…

如何在Linux上使用history命令

Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / ShutterstockLinux’s shell saves a history of the commands you run, and you can search it to repeat commands you’ve run in the past. Once you understand the Linux history command and how to u…

mysql导入sqlserver数据库表

原文:https://zhidao.baidu.com/question/1114325744502691499.html 在Navicat for MySQL 管理器中,创建目标数据库(注意:因为是点对点的数据导入,要求sql server 中要导出的数据库名称和要导入到Mysql 中的数据库的名字相同)点击…

AppleScript

AppleScript 后缀名scpt do shell script "shell脚本"转载于:https://www.cnblogs.com/yangwenhuan/p/10338580.html

ER图三元联系简介

数据库设计时,遇到三元联系怎样确定,下面做个简单介绍。 一、确定联系 三元联系共 4 种情况: 1 : 1 : 11 : 1 : N1 : M : NM : N : P1 &#xff1…

mac自带邮箱导出邮件_如何将电子邮件从Mac Mail导出到Notes应用程序

mac自带邮箱导出邮件Khamosh PathakKhamosh PathakIf you use the Mail app regularly, you’re used to archiving or flagging emails for later. But what if you want to save a particular message for future reference in the Notes app? Well, there’s a work-around…

luogu P3380 【模板】二逼平衡树(树套树)

恭喜你 以分块的姿势通过了此题 #include<cmath> #include<cstdio> #include<algorithm> #define inf (2147483647) using namespace std; const int N5e450; struct FK {int l,r;} K[500]; int n,m,T,sqr,ans,a[N],b[N],fa[N]; void work1(int l,int r,int …

Appium使用Python运行appium测试的实例

Appium使用Python运行appium测试的实例 一&#xff0e; Appium之介绍 https://testerhome.com/topics/8038 详情参考-- https://testerhome.com/topics/8038 Appium是一个移动端的自动化框架&#xff0c;可用于测试原生应用&#xff0c;移动网页应用和混合型应用&#xff0c;且…

ubuntu 任务栏监视器_从系统任务栏监视Google服务

ubuntu 任务栏监视器Are you looking for an app that sits in your System Tray and will notify you when you have new items in your Google accounts? Now you can easily monitor all of your favorite Google services with Googsystray. 您是否正在寻找一个位于系统任…

Java发送邮件(带附件)

实现java发送邮件的过程大体有以下几步&#xff1a; 准备一个properties文件&#xff0c;该文件中存放SMTP服务器地址等参数。利用properties创建一个Session对象利用Session创建Message对象&#xff0c;然后设置邮件主题和正文利用Transport对象发送邮件需要的jar有2个&#x…

google天气预报接口_将天气预报添加到谷歌浏览器

google天气预报接口Are you looking for a quick and easy way to see your local weather forecast in Google Chrome? Then you will definitely want to take a good look at the AccuWeather Forecast extension. 您是否正在寻找一种快速简便的方法来在Google Chrome浏览器…