gpt4 book ai didi

javascript - 在扩展构造函数中调用函数

转载 作者:行者123 更新时间:2023-12-03 05:28:06 26 4
gpt4 key购买 nike

class PathController {
constructor(){

}

getMainPage(){
alert("getMainPage");
}

setPushState(){
alert("setPushState");
}
}

class MainMenu extends PathController {
constructor (){
// call my PathController here
super();
getMainPage();
setPushState();
}
}

let aMainMenu = new MainMenu();

我的目的是在 MainMenu 构造函数中调用 getMainPage 和 setPushState ,我厌倦了 this.getMainPage 和 this.setPushState 并且它也不起作用。谁能告诉我如何调用它?

最佳答案

你的 super 就是你的“this”,因为我们当前处于构造函数中。它应该是这样的:

class PathController {
constructor(){

}

getMainPage(){
alert("getMainPage");
}

setPushState(){
alert("setPushState");
}
}

class MainMenu extends PathController {
constructor (){
// call my PathController here
super();
super.getMainPage();
super.setPushState();
}
}

let aMainMenu = new MainMenu();

但是一旦你在构造函数之外,那么你将使用“this.getMainPage();”

关于javascript - 在扩展构造函数中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41072672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com