super的使用

1、继承而来的类 constructor 方法中的this必须在super之后

2、例1的代码会报错,因为VideoIntercomDevice 调用了super但是super没有传参,是个undefined,undefined去结构就会报错的,纠正的话改为 super({})

3、那我好奇,到底super复制的属性级别更高,还是子类中this指定的值等级更高,见例2,结论:无所谓级别高低,会覆盖掉

例1:

javascript 复制代码
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
相关推荐
菜鸟一枚在这2 小时前
深入解析设计模式之单例模式
开发语言·javascript·单例模式
ObjectX前端实验室2 小时前
个人网站开发记录-引流公众号 & 谷歌分析 & 谷歌广告 & GTM
前端·程序员·开源
CL_IN2 小时前
企业数据集成:实现高效调拨出库自动化
java·前端·自动化
浪九天3 小时前
Vue 不同大版本与 Node.js 版本匹配的详细参数
前端·vue.js·node.js
qianmoQ4 小时前
第五章:工程化实践 - 第三节 - Tailwind CSS 大型项目最佳实践
前端·css
C#Thread4 小时前
C#上位机--流程控制(IF语句)
开发语言·javascript·ecmascript
尚学教辅学习资料4 小时前
基于SpringBoot+vue+uniapp的智慧旅游小程序+LW示例参考
vue.js·spring boot·uni-app·旅游
椰果uu4 小时前
前端八股万文总结——JS+ES6
前端·javascript·es6
微wx笑4 小时前
chrome扩展程序如何实现国际化
前端·chrome
~废弃回忆 �༄4 小时前
CSS中伪类选择器
前端·javascript·css·css中伪类选择器