freeCodeCamp/guide/english/certifications/javascript-algorithms-and-d.../es6/use-class-syntax-to-define-.../index.md

1.2 KiB

title
Use class Syntax to Define a Constructor Function

Use class Syntax to Define a Constructor Function

In this lesson, you are defining the Vegetable object using class syntax.

Hint 1:

Create the class called Vegetable. It will contain the necessary details about the Vegetable object.

Hint 2:

Put a constructor with a parameter called name, and set it to this.name.

Spoiler Alert - Solution Ahead!

Solution:

function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Vegetable {
    constructor(name){
      this.name = name;
    }
  }
  /* Alter code above this line */
  return Vegetable;
}

=======

Spoiler Warning: here is a basic solution to this challenge in case you're stuck.

function makeClass() {
  "use strict";
  /* Alter code below this line */

   class Vegetable {
     constructor(Vegetable){
       this.Vegetable = Vegetable;
  
     }
   }

  /* Alter code above this line */
  return Vegetable;
}
const Vegetable = makeClass();
const carrot = new Vegetable('carrot');
console.log(carrot.name); // => should be 'carrot'