如何解决Uncaught SyntaxError: 'super' 关键字在覆盖方法和继承类上是意外的
我是 Javascript 新手,刚开始学习重写方法和继承类,所以我编写了这段代码并在浏览器控制台上运行它,它只显示'未捕获的语法错误:'super'关键字意外在这里'。 代码如下:
class Mail{
_contacts = [];
constructor(author){
this.from = author;
}
sendMessagge = function(msg,to){
console.log(`You send: ${msg} to ${to} from ${this.from}`);
this._contacts.push(to);
}
showAllContacts(){
return this._contacts;
}};
class whatsApp extends Mail{
constructor(username,isBusinessAccount,phone){
super(phone);
this.username = username;
this.isBusinessAccount = isBusinessAccount;
}
myProfile = function(){
return `username: ${this.username},Type: ${this.isBusinessAccount? 'Business' : 'Personal'},Phone: ${this.from}`;
}
sendMessagge = function(msg,to){
super.sendMessagge(msg,to);
console.log('Sent by WhastApp');
}};
而且我认为子类 whatsApp 上的“sendMessage”方法有问题,因为当我尝试编写这样的代码时:
sendMessagge(){
super.sendMessagge();
console.log('Sent by WhastApp');
}
它在浏览器控制台上运行,但它不像我预期的那样工作(它没有显示“由 whastApp 发送”)。 请给我一些线索,我一直在互联网上寻找解决方案,但我找不到与我完全相同的案例。在此之前,谢谢:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。