ES6:使用 .next() 属性创建类iterationg 属性字段值

问题描述

假设我有一个名为 Frame 的类:

class ScheduleBasedOnTimeZoneJob < ApplicationJob
  queue_as :default

  def perform
    # only execute in the beggening if the day
    return unless Now.to_i - Now.beginning_of_day.to_i < 300

    time_zones = ["America/Los_Angeles","America/Tijuana","America/Phoenix"]
    time_zones.each do |time_zone|
      Now = Time.Now.in_time_zone(time_zone)

      # Schedule job at 08:00 AM based on the provided timezone
      schedule_at = Now.change(hour: 8,min: 0)
      MyTzBasedJob.set(wait_until: schedule_at).perform_later(time_zone: time_zone)
    end
  end
end

而且我想要一个名为 class Frame { constructor({height,width,offset_x,offset_y}) { this.params = { height: height,width: width }; this.offset = { x: offset_x,y: offset_y }; } // ..getters here } 方法,它允许我在每次调用时跳过偏移量。例如:

.next()

我听说过 const TestFrame = new Frame({ height: 10,width: 20,offset_x: 100,offset_y: 100}) const NextFrame = TestFrame.next() console.log(NextFrame) /** * Should give me ( ...{offset_y: 110,offset_x: 120}) * so the new offset_y = offset_y (100) + height (10) * and the offset_x = offset_x (100) + width (20) * */ yield,但不知何故我无法通过 getter 实现它。所以我会很感激你给我的任何建议或例子。

我尝试了什么:

generators

解决方法

您可以在每次读取 next 时创建一个新帧并跟踪创建的帧数。这将允许您在每次读取时应用不断进行的偏移量:

class Frame {
  constructor({height,width,offset_x,offset_y}) {
    this.params = { height: height,width: width };
    this.offset = { x: offset_x,y: offset_y };
    this.offsetCount = 0;
  }
  get center() {
    return { center: { x: (this.params.width - (this.params.width / 2)),y: (this.params.height - (this.params.height / 2)) } };
  }
  get next() {
    this.offsetCount++;
    return new Frame({
      height: this.params.height,width: this.params.width,offset_x: this.offset.x,offset_y: this.offset.y + (this.params.height * this.offsetCount)
    })
  }
}

const CharacterSelect = new Frame({ height: 20,width: 10,offset_x: 100,offset_y: 100 });
console.log(CharacterSelect)
console.log(CharacterSelect.next) //Frame { params: { height: 20,width: 10 },offset: { x: 100,y: 120 } }
console.log(CharacterSelect.next) //Frame { params: { height: 20,y: 140 } }

我建议另一种选择 - 使用生成器函数来生成具有偏移量的无限帧。这使您的 Frame 类不必跟踪所有内容。

class Frame {
  constructor({height,y: offset_y };
  }
  get center() {
    return { center: { x: (this.params.width - (this.params.width / 2)),y: (this.params.height - (this.params.height / 2)) } };
  }
}
const CharacterSelect = new Frame({ height: 20,offset_y: 100 });
const frameGenerator = moreFrames(CharacterSelect);
console.log(CharacterSelect)
console.log(frameGenerator.next().value) //Frame { params: { height: 20,y: 120 } }
console.log(frameGenerator.next().value) //Frame { params: { height: 20,y: 140 } }

function* moreFrames(frame) {
  while(true) {
    const nextFrame = new Frame({
      height: frame.params.height,width: frame.params.width,offset_x: frame.offset.x,offset_y: frame.offset.y + frame.params.height
    });
    yield nextFrame;
    frame = nextFrame;
  }
}

使用一个小助手,您只能从任何可迭代对象中获取有限数量的项目:

function* limit(number,iterable) {
  const it = iterable[Symbol.iterator]();
  
  for(let i = 0,next = it.next();
      i < number && !next.done;
      i++,next = it.next()
  ) {
    yield next.value;
  }  
}

这意味着您一次只能拍摄任意数量的帧:

class Frame {
  constructor({height,y: (this.params.height - (this.params.height / 2)) } };
  }
}

const CharacterSelect = new Frame({ height: 20,offset_y: 100 });
const frameGenerator = moreFrames(CharacterSelect);

const first3 = [...limit(3,frameGenerator)];
const next5  = [...limit(5,frameGenerator)];

console.log(first3);
console.log(next5);

function* moreFrames(frame) {
  while(true) {
    const nextFrame = new Frame({
      height: frame.params.height,offset_y: frame.offset.y + frame.params.height
    });
    yield nextFrame;
    frame = nextFrame;
  }
}

function* limit(number,next = it.next()
  ) {
    yield next.value;
  }  
}