simple-inventory icon indicating copy to clipboard operation
simple-inventory copied to clipboard

Item.extendPrototype() does not work as expected

Open david-donachie opened this issue 10 months ago • 1 comments

Imagine the following sequence:

Item.extendPrototype({ image: "" });
Item.add("Book", { image: "book.png", description: "A book" });
Item.add("Dog", { image: "dog.png", description: "A dog" });
Item.add("Ring", { description: "A Ring" });

This should add an image property to an item, with a default value of an empty string. I would expect this to be set as given on both Book and Dog and blank on Ring.

However this is not the case:

Item.get("Book").image => "book.png"
Item.get("Dog").image => "dog.png"
Item.get("Ring").image => "dog.png"

It appears that the last value supplied for the image property applies to all subsequent Items, as if the default values had been modified.

I suspect the error is here:

Object.assign(this, Object.assign(defaultOpts, opts));

This should probably be:

Object.assign(this, Object.assign({}, defaultOpts, opts));

david-donachie avatar Apr 03 '24 15:04 david-donachie