【问题标题】:Exporting a function using Javascript returns undefined使用 Javascript 导出函数返回未定义
【发布时间】: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


    【解决方案1】:

    您应该在类的实例上调用update,而不是在constructor 上。

    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)

    【讨论】:

    • 哦,我想试试,但不知道怎么做,非常感谢!
    • @KnightPezz 如果满意,请接受答案。
    • OP 在构造函数的原型上调用 update,而不是构造函数。
    • @RobG 没错,这只是语义,因为无论如何它们都不起作用。无论是调用实际的构造函数还是它的原型。不过感谢您的澄清。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2021-01-31
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多