Javascript 如何在类构造中实现各种方法 上课时间{ 建造师(小时,分钟){ 这个.小时=小时; 这个。分钟=分钟; } addTime(val=0,func=showTime){//我怎么也能写这个部分呢?60){ 这个.hour++; 这个。分钟-=60; } return this.func()//这是我一直在玩弄的
尝试将Javascript 如何在类构造中实现各种方法 上课时间{ 建造师(小时,分钟){ 这个.小时=小时; 这个。分钟=分钟; } addTime(val=0,func=showTime){//我怎么也能写这个部分呢?60){ 这个.hour++; 这个。分钟-=60; } return this.func()//这是我一直在玩弄的,javascript,class,Javascript,Class,尝试将return this.func()更改为return func()如果您想在另一个函数中访问同一类的函数,我建议使用self参数。例如def function\u name(self,other\u参数)。这里的“self”表示类,然后您可以使用self.function\u name调用其他函数 因此,在您的程序中,您可以使用以下代码调用另一个函数中的其他函数: class Time{ constructor(hours,minutes){ this.hours
return this.func()
更改为return func()
如果您想在另一个函数中访问同一类的函数,我建议使用self
参数。例如def function\u name(self,other\u参数)
。这里的“self”表示类,然后您可以使用self.function\u name
调用其他函数
因此,在您的程序中,您可以使用以下代码调用另一个函数中的其他函数:
class Time{
constructor(hours,minutes){
this.hours=hours;
this.minutes=minutes;
}
addTime(val=0,func=showTime){ //How can I write this part too?<--
this.minutes+=val;
if(this.minutes>60){
this.hour++;
this.minutes-=60;
}
return this.func() //this was something I was toying with<--
}
round(){
switch(this.minutes%5){
case 0:break;
case 1:this.minutes-=1;break;
case 2:this.minutes-=2;break;
case 3:this.minutes+=2;break;
case 4:this.minutes+=1;break;
default:break;
}
//console.log(this.minutes)
return this.hours+":"+this.minutes; //here too... I didn't know how to return a method<--
}
showTime(){
return this.hours+":"+this.minutes;
}
}
欢迎使用Stackoverflow@abhiCoder。请注意,如果您在某个特定代码问题上遇到了其他资源无法解决的问题,但您的问题似乎是一个基本的编程问题,那么此社区将为您提供许多问题。请尝试了解公开可用的资源,如
def func1(self):
#code
def func2(self):
self.func1