super的使用
创始人
2024-11-14 16:05:43

1、继承而来的类 constructor 方法中的this必须在super之后
2、例1的代码会报错,因为VideoIntercomDevice 调用了super但是super没有传参,是个undefined,undefined去结构就会报错的,纠正的话改为 super({})
3、那我好奇,到底super复制的属性级别更高,还是子类中this指定的值等级更高,见例2,结论:无所谓级别高低,会覆盖掉
例1:

class Config {   constructor() {     console.log('Config调用')     this.id = 0;   } } class AccessDevice extends Config {   constructor({ id, isCsJump }) {     console.log('AccessDevice')     super();     this.id = id;     this.isCsJump = isCsJump;   } } class VideoIntercomDevice extends AccessDevice {   constructor({ id, isCsJump }) {     console.log('1', id, isCsJump)     super()     console.log('id', id)     this.id = 9;     this.isCsJump = isCsJump;   } } let ad = new VideoIntercomDevice({id: 1,isCsJump: true}) console.log(ad)  // 最终会打印:1 1 true 

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...