英文:
Possible to convert this OOP clock to a function?
问题
我在我的纯函数项目中有以下类。这是唯一的面向对象部分,我希望如果可能的话创建一个不需要实例化的函数。
我这样使用它:
const timer = new Clock(1);
await timer.start();
const coolDownTimer = new Clock(2);
const globalTimer = new Clock(3);
await globalTimer.start();
问题
将时钟设为面向对象是我能想到的唯一方法,所以我想知道是否可以在不创建Clock类实例的情况下实现?
const Clock = function (checkInterval) {
  // ...(原始代码)
};
module.exports = Clock;
英文:
I have the below class in my otherwise pure function project. It is the only part that is OOP, and I and would like to make a function that isn't instantiated if possible.
I use it like so
const timer = new Clock(1);
await timer.start();
const coolDownTimer = new Clock(2);
const globalTimer = new Clock(3);
await globalTimer.start();
Question
Making the clock OOP was the only way I could think of, so I am wondering weather it would have been possible without making an instance of the Clock class?
const Clock = function (checkInterval) {
  let offset;
  let clock;
  let interval;
  // initialize
  reset();
  function start() {
    if (!interval) {
      offset = Date.now();
      interval = setInterval(update, checkInterval);
    };
  };
  function update() {
    clock += delta();
  };
  function delta() {
    const now = Date.now();
    const d = now - offset;
    offset = now;
    return d;
  };
  function stop() {
    if (interval) {
      clearInterval(interval);
      interval = null;
    };
  };
  function reset() {
    clock = 0;
  };
  function read() {
    return clock;
  };
  // public API
  this.start = start;
  this.stop = stop;
  this.reset = reset;
  this.read = read;
};
module.exports = Clock;
答案1
得分: 1
将Clock函数修改为返回一个包含时钟状态的简单对象。然后,将方法替换为普通函数,这些函数以此对象作为参数并访问其属性。
需要导出所有应由客户端调用的函数。在下面的示例中,我导出了它们所有,但你可能会发现将其中一些保留为内部函数更合适。
function Clock(checkInterval) {
    let self = {
        offset: null,
        clock: null,
        interval: checkInterval
    };
    clock_reset(self);
    return self;
}
function clock_start(clock) {
    if (!clock.interval) {
        clock.offset = Date.now();
        clock.interval = setInterval(() => clock_update(clock), clock.interval);
    };
};
function clock_update(clock) {
    clock.clock += clock_delta(clock);
};
function clock_delta(clock) {
    const now = Date.now();
    const d = now - clock.offset;
    clock.offset = now;
    return d;
};
function clock_stop(clock) {
    if (clock.interval) {
        clearInterval(clock.interval);
        clock.interval = null;
    };
};
function clock_reset(clock) {
    clock.clock = 0;
};
function clock_read(clock) {
    return clock.clock;
};
module.exports = {
    Clock,
    clock_start,
    clock_update,
    clock_delta,
    clock_stop,
    clock_reset,
    clock_read
};
英文:
Have the Clock function return a simple object containing the state of the clock. Then replace the methods with ordinary functions that take one of these objects as a parameter, and access the properties.
You'll need to export all the functions that should be callable by clients. In my example below I export them all, you may find it more appropriate to keep some of them internal.
function Clock(checkInterval) {
	let self = {
		offset: null,
		clock: null,
		interval: checkInterval
	};
	clock_reset(self);
	return self;
}
function clock_start(clock) {
    if (!clock.interval) {
		clock.offset = Date.now();
		clock.interval = setInterval(() => clock_update(clock), clock.interval);
    };
};
function clock_update(clock) {
    clock.clock += clock_delta(clock);
};
function clock_delta(clock) {
    const now = Date.now();
    const d = now - clock.offset;
    clock.offset = now;
    return d;
};
function clock_stop(clock) {
    if (clock.interval) {
		clearInterval(clock.interval);
		clock.interval = null;
    };
};
function clock_reset(clock) {
    clock.clock = 0;
};
function clock_read(clock) {
    return clock.clock;
};
module.exports = {
	Clock,
	clock_start,
	clock_update,
	clock_delta,
	clock_stop,
	clock_reset,
	clock_read
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论