diff --git a/packages/loki/spec/generic/cloning.spec.js b/packages/loki/spec/generic/cloning.spec.js index af8fd4e4..25c5a70d 100644 --- a/packages/loki/spec/generic/cloning.spec.js +++ b/packages/loki/spec/generic/cloning.spec.js @@ -65,6 +65,29 @@ describe("cloning behavior", () => { }); }); + describe("cloning method \"shallow\" save prototype", function () { + it("works", () => { + function Item(name, owner, maker) { + this.name = name; + this.owner = owner; + this.maker = maker; + } + + const cdb = new loki("clonetest"); + const citems = cdb.addCollection("items", {clone: true, cloneMethod: "shallow"}); + const oldObject = new Item("mjolnir", "thor", "dwarves"); + const insObject = citems.insert(oldObject); + + // cant' have either of these polluting our collection + oldObject.name = "mewmew"; + insObject.name = "mewmew"; + + const result = citems.findOne({"owner": "thor"}); + expect(result instanceof Item).toBe(true); + expect(result.name).toBe("mjolnir"); + }); + }); + describe("collection find() cloning works", () => { it("works", () => { const cdb = new loki("cloningEnabled"); diff --git a/packages/loki/src/clone.js b/packages/loki/src/clone.js index b5345091..5c623c41 100644 --- a/packages/loki/src/clone.js +++ b/packages/loki/src/clone.js @@ -16,14 +16,14 @@ export function clone(data, method) { break; case "shallow": // more compatible method for older browsers - cloned = data.prototype ? Object.create(data.prototype) : {}; + cloned = Object.create(data.constructor.prototype); Object.keys(data).map((i) => { cloned[i] = data[i]; }); break; case "shallow-assign": // should be supported by newer environments/browsers - cloned = data.prototype ? Object.create(data.prototype) : {}; + cloned = Object.create(data.constructor.prototype); Object.assign(cloned, data); break; default: