Question about "this" in new constructor

Tell us what’s happening:
Describe your issue in detail here.

The problem only passes if you pass the name and color as properties of the variable name of the new constructor. I was wondering why this can’t be used instead of the variable name (in this case “terrier”).

My assumption is that this would be used at a more global scale and the variable name focuses the scope to the specific Dog() object being described. Is this correct?

If it IS possible to use this in the situation described above, then why would my code not pass when using it?
Your code so far


function Dog(name, color) {
this.name = name;
this.color = color;
this.numLegs = 4;
}
let terrier = new Dog();
this.name = "Jack";
this.color = "black and white";


  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Extend Constructors to Receive Arguments

Link to the challenge:

Hello!

It does work. The problem you have is that you used this outside the context of the function, in which case it refers to a different context (the window).

To fix it, replace the assignment this.name = "Jack" and this.color with the actual instance of the object: terrier.

Hope it helps :slight_smile:.

Think that there is multiple constructor functions. If “this” is global like you said which constructor function it refers to.

globally “this” means the window object in the browser
and inside and object “this” refers to the object itself

1 Like

Did you research the this keyword?


I have noticed you seem to be making a lot of assumptions and speculations instead of actually looking up how things work…you know what they say about assumptions.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.