为什么myTodos.meetDone()函数返回Nan? 它当然是js我只是定义一个名为myTodos的对象,而我只想打印console.log()结果。
let myTodos = {
day: 'Monday',
meetings: 0,
meetDone: 0,
addMeeting: function (num) {
this.meetings = this.meetings + num;
},
meetDone: function (num) {
this.meetDone = this.meetDone - num;
},
resetDay: function () {
this.meetings = 0;
this.meetDone = 0;
},
report: function () {
let meetleft = this.meetings + this.meetDone;
return `Yo have ${meetleft} meeting for today!`;
},
}
myTodos.addMeeting(2);
myTodos.addMeeting(5);
myTodos.meetDone(23);
console.log(myTodos);
console.log(myTodos.report());
首先,console.log用于调试 为什么它回归南我不理解这个!!!!
答案 0 :(得分:2)
正如@AndrewBone在评论中已经提到的,您的对象有两个名为meetDone
的属性。后者覆盖了第一个,因此当您添加this.meetings + this.meetDone
时,您将向一个数字添加一个函数,导致显示NaN。更改其中一个属性的名称,meetingDone
比meetDone
IMO更具描述性
let myTodos = {
day: 'Monday',
meetings: 0,
meetDone: 0,
addMeeting: function (num) {
this.meetings = this.meetings + num;
},
meetingDone: function (num) {
this.meetDone = this.meetDone - num;
},
resetDay: function () {
this.meetings = 0;
this.meetDone = 0;
},
report: function () {
let meetleft = this.meetings + this.meetDone;
return `Yo have ${meetleft} meeting for today!`;
},
}
myTodos.addMeeting(2);
myTodos.addMeeting(5);
myTodos.meetingDone(23);
console.log(myTodos);
console.log(myTodos.report());