diff --git a/spec/generic/cloning.spec.js b/spec/generic/cloning.spec.js index 874edc6a..c811c2b7 100644 --- a/spec/generic/cloning.spec.js +++ b/spec/generic/cloning.spec.js @@ -63,7 +63,31 @@ describe('cloning behavior', function () { expect(result.name).toBe("mjolnir"); }); }); - + + describe('cloning method "shallow" save prototype', function() { + it('works', function() { + function Item(name, owner, maker) { + this.name = name; + this.owner = owner; + this.maker = maker; + } + + var cdb = new loki('clonetest'); + var citems = cdb.addCollection('items', { clone: true, cloneMethod: "shallow" }); + var oldObject = new Item('mjolnir', 'thor', 'dwarves'); + var insObject = citems.insert(oldObject); + + // cant' have either of these polluting our collection + oldObject.name = 'mewmew'; + insObject.name = 'mewmew'; + + var result = citems.findOne({'owner': 'thor'}); + + expect(result instanceof Item).toBe(true); + expect(result.name).toBe("mjolnir"); + }); + }); + describe('collection find() cloning works', function() { it('works', function () { var cdb = new loki('cloningEnabled'); diff --git a/src/lokijs.js b/src/lokijs.js index aa64b6f7..167e2dad 100644 --- a/src/lokijs.js +++ b/src/lokijs.js @@ -558,14 +558,14 @@ 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(function (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: