导出使用Javascript返回未定义功能

问题描述 投票:1回答:1

正如前言;我开始学习JavaScript大约3天前,所以我对一些事情的要求理解不完全存在。

所以,我有一个程序,我运行使用一个时钟,使文件夹,文件和日志带有时间戳。但是,当我打电话的时候,从它只是返回函数内undefined @ undefined:undefined:NaNwhereas的功能之外,它返回像正常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个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.