gpt4 book ai didi

Javascript:无法调用 getter

转载 作者:行者123 更新时间:2023-11-30 08:22:12 25 4
gpt4 key购买 nike

class Clock {
constructor() {
this.schedule = [];
this.simulationTime = 0;
}

get isEmpty() {
return this.schedule == false;
}

popFirstItem(){
if(isEmpty){
throw "error";
}
}
};

我想在 popFirstItem() 方法中调用 getter isEmpty()。但是,我做不到。在popFirstItem() 方法中调用isEmpty() 的正确方法是什么?

最佳答案

你需要使用this.isEmptythis 将始终引用该类,使用该引用您可以调用类方法或属性:

class Clock {
constructor() {
this.schedule = [];
this.simulationTime = 0;
}

get isEmpty() {
console.log('Inside isEmpty()');
return this.schedule == false;
}

popFirstItem(){
if(this.isEmpty){
throw "error";
}
}
};
var clock = new Clock();
console.log(clock.popFirstItem());

关于Javascript:无法调用 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51589857/

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