0

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()

aryan
  • 37
  • 3
  • You can't assign to `this`. It's not a variable, it's a special keyword that holds the context of the call. – Barmar May 08 '17 at 15:55
  • 1
    There are a couple of misconceptions here (that's why I'm not sure this is a duplicate): a) You cannot assign to `this`, it's a syntax error. Consider it read-only. b) `Product` doesn't return `this` (there is no `return this;` inside the function). It's actually `new` that returns the new instance. Since you are not calling `new Product(...)`, nothing is returned. c) Objects are represented as references. Meaning that if `Product.call(this, ...)` is called, `this` inside `Product` refers to the same object as `this` inside `Food`. Hence all changes are made to the `Food` instance. – Felix Kling May 08 '17 at 16:01
  • @FelixKling, thanks for the answer. I am aware that "this" is read only, i just wanted to illustrated that in my mind there should be an assignment operation on "this" of Food(); Would it be right to say then that "this" is being passed as reference to Product.call() ? – aryan May 08 '17 at 16:07
  • @user2744643 The "assignment" takes place only in ES6 classes with `super()`. – Bergi May 08 '17 at 16:42
  • [*Pass by reference*](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference) has a different meaning, that's not what is happening here (but it's a common misconception). Rather it is passing *a* reference (`this` holds a reference to an object). Here is very simply example of what's happening: `function addBar(obj) { obj.bar = 42; }; var foo = {}; addBar(foo); console.log(foo);`. The object referenced by `foo` will have the property `bar` after `addBar` was called. You can think of `this` being a special parameter. – Felix Kling May 08 '17 at 17:17
  • thanks @FelixKling, that cleared everything up. I am not sure if i can accept a comment as an answer, but if you could post it as an answer i will accept it as an answer. – aryan May 08 '17 at 18:03

0 Answers0