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
相关推荐
lvchaoq21 分钟前
this指向面试代码输出题目合集
javascript·面试·职场和发展
trigger33324 分钟前
desk-health-web-community-post
前端
倒流时光三十年25 分钟前
Logback 系列(7):常用 Appender(控制台 / 文件 / 滚动文件)
java·前端·logback
Csvn1 小时前
页面「穿越」之谜:React 中因 key 值不正确导致的渲染 Bug 排查实战
前端
浅水壁虎1 小时前
前端技术_TypeScript(第一章)
前端
你怎么知道我是队长1 小时前
JavaScript 函数 this 全解
开发语言·javascript·ecmascript
laboratory agent开发2 小时前
企业AI Agent落地前,先回答四个工程问题
java·前端·人工智能
微三云 - 廖会灵 (私域系统开发)2 小时前
电商系统国际化架构设计:多语言、多币种、多时区、多税制的全链路实现
java·前端·数据库
不简说3 小时前
JS 代码技巧 vol.5 — 10 个错误处理套路,从 try/catch 到 Error Boundary
前端·javascript·程序员
wordpress资料库3 小时前
Next.js更适合移动开发 不适合web开发
开发语言·前端·javascript·next.js