我正在执行摘要功能,该功能将显示myToDos的摘要,但是在控制台中它显示“您今天有NaN会议!”而我正在使用数字类型的所有变量。
let myTodos={
day: 'Monday',
meetings: 0,
meetDone: 0,
addMeeting: function(num){
this.meetings = this.meetings + num;
},
meetDone: function(meet=0) {
this.meetDone = this.meetDone + meet;
},
resetDay: function(){
this.meetings = 0;
this.meetDone = 0;
},
summary: function(){
let leftmeetings = this.meetings - this.meetDone;
return `You have ${leftmeetings} meetings today!`
}
}
myTodos.addMeeting(5);
myTodos.meetDone(3);
console.log(myTodos.summary());
我希望输出为
javascript
"You have 2 meetings today!"
答案 0 :(得分:3)
如注释中所述,您具有重复的属性。
将meetDone
重命名为done
将消除冲突。
let myTodos = {
day: "Monday",
meetings: 0,
done: 0,
addMeeting: function(num) {
this.meetings = this.meetings + num;
},
meetDone: function(meet = 0) {
this.done = this.done + meet;
},
resetDay: function() {
this.meetings = 0;
this.meetDone = 0;
},
summary: function() {
let leftmeetings = this.meetings - this.done;
return `You have ${leftmeetings} meetings today!`;
}
};
myTodos.addMeeting(5);
myTodos.meetDone(3);
console.log(myTodos.summary());
输出:
您今天有2次会议!