【发布时间】:2019-07-04 07:09:19
【问题描述】:
就像前言一样;我大约 3 天前开始学习 Javascript,所以我对某些事情的要求的理解并不完全。
所以,我正在运行一个程序,它使用时钟来制作带有时间戳的文件夹、文件和日志。但是当我从函数外部调用时间时,它只返回undefined @ undefined:undefined:NaN而在函数内部它返回的时间就像正常的10/2/2019 @ 11:6:45
function updateClock() {
var currentdate = new Date();
var day = currentdate.getDate();
var month = currentdate.getMonth() + 1;
var year = currentdate.getFullYear();
this.date = " " + day + "/"
+ month + "/"
+ year;
this.h = currentdate.getHours();
this.m = currentdate.getMinutes();
this.s = currentdate.getSeconds();
}
updateClock.prototype.run = function () {
setInterval(this.update.bind(this), 1000);
};
updateClock.prototype.update = function () {
this.updateTime(1);
var time = " @ "
+ this.h + ":"
+ this.m + ":"
+ this.s;
var datetime = this.date + time;
console.log(datetime);
return datetime;
};
updateClock.prototype.updateTime = function (secs) {
this.s += secs;
if (this.s >= 60) {
this.m++;
this.s = 0;
};
if (this.m >= 60) {
this.h++;
this.m = 0;
};
if (this.h >= 24) {
this.day++;
this.h = 0;
}
};
var newclock = new updateClock();
newclock.run();
var timedate = updateClock.prototype.update();
console.log(timedate)
做什么以及如何解决?谢谢,我很感激!
【问题讨论】:
标签: javascript date undefined clock