I am trying to understand how "this" works when using call as a chain of constructors.
In the following snippet from MDN:
function Product(name, price) {
this.name = name;
this.price = price;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
var cheese = new Food('feta', 5);
Shouldn't the statement
Product.call(this, name, price);
be
this = Product.call(this, name, price);
(EDIT: i know "this" is readonly, i just want to illustrate that in my mind there should be an assignment operation on "this" of Food().)
From what i understand the use of new at :
var cheese = new Food('feta', 5);
creates a new empty object which is being pointed at "this" during the execution of the Food() function.
Then we are calling the Product() function with the "this" object. Product() will return the modified this object, but it is not being saved anywhere in Food()
So how is the "this" in Food() being updated with the returning "this" from Product()