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
相关推荐
午安~婉4 分钟前
HTML CSS八股
前端·css·html
有事没事实验室6 分钟前
css变量
前端·css
前端付豪17 分钟前
Vue 中的 JSX:让组件渲染更灵活的正确方式
前端·javascript·vue.js
在泡泡里18 分钟前
前端规范【四】eslint(antfu)、lefthook、commitlint
前端
烛阴19 分钟前
Python 几行代码,让你的照片秒变艺术素描画
前端·python
Jolyne_43 分钟前
如何实现无感刷新Token
前端
用户4099322502121 小时前
Vue3响应式系统的底层原理与实践要点你真的懂吗?
前端·ai编程·trae
apollo_qwe1 小时前
Vue 权限控制神技!自定义 auth 指令优雅实现按钮级权限管理
vue.js·架构
qq_479875431 小时前
RVO和移动语义
前端·算法
加菲喵1 小时前
深度解析:在vue3中使用自定义Hooks
前端