[STRANG RESULTS] Use class Syntax to Define a Constructor Function

I really need some help. Heres my code:

 function makeClass() {
  class Vegetable {
    constructor (name) { this.name = name; }}
  return Vegetable;}

const Vegetable = makeClass();
const carrot = new Vegetable('carrot');
console.log(carrot.name); // => should be 'carrot'

When i type in console.log(carrot.name); I get “Vegetable” .
When I put console.log(carrot); I get
class Vegetable {
prototype: Vegetable

carrot.name is returning ‘carrot’ from your code.

Yes, console logging carrot will return the definition of your class. This is expected behavior. When you use new, it instantiates an object.

1 Like