Экспорт функции с использованием Javascript возвращает неопределенное - PullRequest
0 голосов
/ 10 февраля 2019

Так же, как предисловие;Я начал изучать Javascript около 3 дней назад, и поэтому мое понимание требований к некоторым вещам не полностью там.

Итак, у меня есть программа, которую я запускаю, которая использует часы для создания папок, файлов ижурналы с отметкой времени.Но когда я вызываю время извне функции, оно просто возвращает 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)

Что делать и как исправить?Спасибо, я ценю это!

1 Ответ

0 голосов
/ 10 февраля 2019

Вы должны вызывать 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...