Use class Syntax to Define a Constructor Function - Explanation please

I answered this challenge correctly but I don’t actually understand how it works, any simple explanation please.

I don’t understand what “class & constructor & this” does…

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

Allow me to explain the following terms.

class - defines a blueprint for an object in code. An object contains a group of functions and variables; allowing for proper organization.

constructor - it essentially constructs or builds a new object instance.

this - refers to the instance of the object the method was called on. Not every variable is the same for every version of a class , so this is why we use the keyword this. Using the challenge as a guide, this prevents every Vegetable from becoming a carrot.

2 Likes
2 Likes

I didn’t quite understand this

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.