I'm trying to understand chaining constructors with javascript's call function. I'm looking at an example from here.
I've copied and pasted the example:
function Product(name, price) {
this.name = name;
this.price = price;
if (price < 0)
throw RangeError('Cannot create product "' + name + '" with a negative price');
return this;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
Food.prototype = Object.create(Product.prototype);
function Toy(name, price) {
Product.call(this, name, price);
this.category = 'toy';
}
Toy.prototype = Object.create(Product.prototype);
var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);
What I'm confused about is that the call function for Food and Toy is called in their "constructor", but the object is not created until we called Object.create(Product.prototype) a few lines down... or so I thought.
Where exactly is the object being inherited created here? And what exactly is happening when we do Product.call? Is it creating an instance of Product? How does that relate to Object.create(Product.prototype)?