Use class Syntax to Define a Constructor Function. Why is function returning value without parameters?

Tell us what’s happening:

I didn’t understand the third last line (const Vegetable = makeClass();). What is its purpose ?

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/71.0.3578.98 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

the function makeClass creates and returns a template of the Vegetable class. so the line,
const Vegetable = makeClass() is assigning the constant variable to the return value of makeClass–a general Vegetable.

then carrot is assigned to a new specific Vegetable.

1 Like

There wasn’t specific need of putting class in makeClass function, Why is it typed in function ?

When the code run, the wrapper function executes immediately and returns the class you are defining along with the “use strict”. It is for the benefit of the tests, so that the code always runs in strict mode.

1 Like

Yeah, that must be the case. Thanks