使用 Javascript returns undefined 导出函数
Exporting a function using Javascript returns undefined
作为前言;我大约 3 天前开始学习 Javascript,所以我对某些事情的要求的理解并不完全。
所以,我有一个程序,我是 运行,它使用时钟来制作带有时间戳的文件夹、文件和日志。但是当我从函数外部调用时间时它只是 returns undefined @ undefined:undefined:NaN
而在函数内部它 returns 时间就像正常的 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)
做什么以及如何修复?谢谢,我很感激!
您应该在 class 而不是 constructor
的实例上调用 update
。
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 = newclock.update();
console.log(timedate)
作为前言;我大约 3 天前开始学习 Javascript,所以我对某些事情的要求的理解并不完全。
所以,我有一个程序,我是 运行,它使用时钟来制作带有时间戳的文件夹、文件和日志。但是当我从函数外部调用时间时它只是 returns undefined @ undefined:undefined:NaN
而在函数内部它 returns 时间就像正常的 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)
做什么以及如何修复?谢谢,我很感激!
您应该在 class 而不是 constructor
的实例上调用 update
。
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 = newclock.update();
console.log(timedate)