Make Instances of Objects with a Constructor Function (right code)

Make Instances of Objects with a Constructor Function (right code)
0

#1

Tell us what’s happening:
Not sure if this is right, but it works.

I’m feeling like I shouldn’t have add:
myCar.wheels = 4;

to make it work… Any other code that works?

Your code so far

var Car = function() {
  this.wheels = 4;
  this.engines = 1;
  this.seats = 5;
};

// Only change code below this line.

var myCar = function() {
  this.wheels = 4;
  this.engines = 1;
  this.seats = 5;
};

myCar.nickname = "Betsy";
myCar.wheels = 4;

Your browser information:

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

Link to the challenge:


#2

It looks like a bug. myCar should be assigned the value of new Car(), not a new function. By assigning it new Car(), it will get the properties wheel, engines and seats witht their respective values in the Car function.


#3

To use a constructor function we call it with the new keyword in front of it like:

var myCar = new Car();

And following this way the code will be like:
var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 5;
};

// Only change code below this line.

var myCar = new Car();
myCar.nickname = “Tesla”;