Skip to content

JewishDate.clone() hardcodes new JewishDate, so cloning a JewishCalendar loses subclass methods (getParsha throws) #36

Description

@Hameishiv

Summary

JewishDate.prototype.clone() constructs new JewishDate(...) rather than new this.constructor(...). Because JewishCalendar extends JewishDate and does not override clone(), calling clone() on a JewishCalendar instance returns a plain JewishDate — losing every JewishCalendar method. Any code that clones a calendar and then calls a subclass method (e.g. getParsha()) throws.

Source

dist/kosher-zmanim.js (v0.9.0), JewishDate.prototype.clone:

JewishDate.prototype.clone = function () {
    var clone = new JewishDate(this.jewishYear, this.jewishMonth, this.jewishDay);
    clone.setMoladHours(this.moladHours);
    clone.setMoladMinutes(this.moladMinutes);
    clone.setMoladChalakim(this.moladChalakim);
    return clone;
};

Minimal reproduction

const KZ = require('kosher-zmanim');
const jc = new KZ.JewishCalendar(new Date('2026-07-25T12:00:00'));
jc.setInIsrael(false);
const c = jc.clone();
console.log(c instanceof KZ.JewishCalendar); // false  (expected true)
console.log(typeof c.getParsha);            // undefined
c.getParsha();                              // TypeError: c.getParsha is not a function

Expected

clone() on a JewishCalendar should return a JewishCalendar (subclass-preserving), so inherited/consuming methods that rely on clone() don't break.

Suggested fix

Use the runtime constructor so subclasses are preserved, e.g.:

JewishDate.prototype.clone = function () {
    var clone = new this.constructor(this.jewishYear, this.jewishMonth, this.jewishDay);
    clone.setMoladHours(this.moladHours);
    clone.setMoladMinutes(this.moladMinutes);
    clone.setMoladChalakim(this.moladChalakim);
    return clone;
};

(Alternatively override clone() in JewishCalendar to also carry inIsrael/useModernHolidays state.)

Found while building a daily-Chumash schedule that needed an upcoming-parsha forward search; worked around it by iterating over fresh JewishCalendar instances instead of cloning. Thanks for the library!

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions