У меня есть этот код, я пытаюсь экспортировать его в другой файл js, и могу спокойно сказать, что понятия не имею, как это сделать. вот код ниже. Я хотел бы выполнить эту временную функцию в другом файле JavaScript. До сих пор я пробовал так много форм, но безрезультатно, любые предложения приветствуются. Спасибо.
function my_Clock()
{
this.cur_date = new Date();
this.hours = this.cur_date.getHours();
this.minutes = this.cur_date.getMinutes();
this.seconds = this.cur_date.getSeconds();
}
my_Clock.prototype.run = function ()
{
setInterval(this.update.bind(this), 1000);
};
my_Clock.prototype.update = function ()
{
this.updateTime(1);
console.info(this.hours + ":" + this.minutes + ":" + this.seconds);
};
my_Clock.prototype.updateTime = function (secs)
{
this.seconds+= secs;
if (this.seconds >= 60)
{
this.minutes++;
this.seconds= 0;
}
if (this.minutes >= 60)
{
this.hours++;
this.minutes=0;
}
if (this.hours >= 24)
{
this.hours = 0;
}
};
var clock = new my_Clock();
clock.run();
я сделал это:
module.exports = {
my_Clock: function () {
this.cur_date = new Date();
this.hours = this.cur_date.getHours();
this.minutes = this.cur_date.getMinutes();
this.seconds = this.cur_date.getSeconds();
}
}
module.exports= {
my_Clock: function () {
my_Clock.prototype.run = function () {
setInterval(this.update.bind(this), 1000);
}
}
}
module.exports= {
my_Clock: function () {
my_Clock.prototype.update = function () {
this.updateTime(1);
return this.hours + "-" + this.minutes + "-" + this.seconds;
}
}
}
module.exports= {
my_Clock: function () {
my_Clock.prototype.updateTime = function (secs) {
this.seconds += secs;
if (this.seconds >= 60) {
this.minutes++;
this.seconds = 0;
}
if (this.minutes >= 60) {
this.hours++;
this.minutes = 0;
}
if (this.hours >= 24) {
this.hours = 0;
}
}
}
}
я пытаюсь выполнить это в другом файле
const myModule = require('../functions/timefunction');
var clock = new myModule.my_Clock().my_Clock();
clock.run();
Пожалуйста, помогите кому-нибудь !!!!!
Вы, сэр, гений, это было так быстро, любители. это работает, еще одна вещь, я использовал console.info для вывода времени, я пытался использовать return, чтобы вернуть жало, но он продолжает возвращать "undefined", любая идея доброго сэр
Большое спасибо @SamiHult, я наконец-то получил работу, мне пришлось использовать обратные вызовы интересно.



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Учитывая, что ваш
my_Clockработает должным образом (первый блок кода в сообщении), вы просто пытались сказатьmodule.exports = my_Clock;, а затемconst my_Clock = require(pathToThatFile); const clock = new myClock();?