Class继承

Class继承1.简介2. super关键字3. 类的prototype和__proto__属性4. 原生构造函数的继承5. Mixin 模式实现1.简介extends实现继承super表示父类的构造函数子类在constructor中,必须调用super方法,this必须在super方法后使用(通过父类构造函数,完成this对象的塑造)es5继承:先生成this,然后通过Parent....
摘要由CSDN通过智能技术生成

1.简介

  • extends实现继承
  • super表示父类的构造函数
  • 子类在constructor中,必须调用super方法,this必须在super方法后使用(通过父类构造函数,完成this对象的塑造)
  • es5继承:先生成this,然后通过Parent.apply(this),继承属性
  • 子类 继承父类静态方法
  • 获取父类:Object.getPrototypeOf
class Parent{
   
	constructor(data){
   
		this.pdata = data
	}
	toString(){
   
		console.log(this == c)		//true
		return String(this.pdata)
	}
	static say(){
   
		return 'hi'
	}
}
class Child extends Parent{
   
	constructor(pdata, data){
   
		//调用父类的构造函数
		super(pdata)		
		this.data = data
	}
	toString(){
   
		return String(this.data + ' ' + super.toString())
	}
	static childSay(){
   
		return super.say()
	}
}

const c = new Child('parent','child')
c.toString()		//"child parent"
c					//Child {pdata: "parent", data: "child"}
Child.prototype.toString == c.toString	//true
c instanceof Child			//true
c instanceof Parent			//true
Child.say()					//hi
Child.childSay()				//hi
Object.getPrototypeOf(Child) == Parent		//true

2. super关键字

  • 当函数使用,代表父类的构造函数
    • super内部this,指向子类实例
    • 相当于A.prototype.constructor.call(this)
    • 作为函数时,super只能用于,子类构造函数的调用,其他地方使用报错
class A
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值