[HELP]Use class Syntax to Define a Constructor Function

[HELP]Use class Syntax to Define a Constructor Function
0

#1

Tell us what’s happening:
I managed to pass this test with the following code but still, I don’t know how this code works😒

I have two questions, please explain to me if you can.

1)What is the purpose of using makeClass() function here? and Why we are assigning it to const Vegetable?

2)How does constructor() works? and what does the following code indicates?

constructor(name)
{
this.name=name;
}

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
    class Vegetable{
      constructor(name){
        this.name=name;
      }
    }
  /* Alter code above this line */
  return Vegetable;
}
const Vegetable = makeClass();
const carrot = new Vegetable('carrot');
console.log(carrot.name); // => should be 'carrot'

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function


#2
  1. Usually classes are self-standing and aren’t nested inside in another wrapper function. makeClass function was created probably just to show constructor can be wrapped.

  2. constructor is basically a factory. You can pass in whatever parameters inside and it will customize their products with corresponding variables. That code particular accepts a name parameter so you can give it any name and that instance of class will have that name.

Look here for more info.