欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > super的使用

super的使用

2024/10/24 5:24:18 来源:https://blog.csdn.net/weixin_43131046/article/details/140881914  浏览:    关键词:super的使用

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com