freeCodeCamp Challenge Guide: Make Unique Objects by Passing Parameters to our Constructor

freeCodeCamp Challenge Guide: Make Unique Objects by Passing Parameters to our Constructor
0.0 0

#1

When you have a constructor but don’t want to keep creating the same object, all you have to do is add parameters to the constructor the following way:

var Car = function(wheels, seats, engines) {

  this.wheels = wheels;

  this.seats = seats;

  this.engines = engines;

};

Now you can pass in arguments when you call the constructor. var myCar = new Car(6, 3, 1);

This will result in:

{

  wheels: 6,

  seats: 3,

  engines: 1

}

#2

this is what I have, but it’s not working, anybody able to tell me what I’m doing wrong?

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

//Try it out here
var myCar = new Car(3,1,2);


#3

var Car = function(wheels, seats, engines) {
//Change this constructor
this.wheels = wheels;
this.seats = seats;
this.engines = engines;
};

//Try it out here
var myCar = new Car(3,1,2);


#4

whats up i had the same problem check out this link to get an answer to this problem https://www.w3schools.com/js/js_object_definition.asp

scroll down to

“Using an Object Constructor”

about midway the page.

pretty much apart from adding them to the parameter we must also do someething like below

this.wheels= wheels

hope this helps


#5

Took me awhile to figure out but make sure to go back a few lessons and hone your skills in creating new objects.