What is the benefit?

Hello,

I’m not really understanding the benefit of classes and constructors, let alone know what a constructor and this. is.

Can someone explain why one would use classes instead of simply using the code below

var test = (name) => {

  return {

    myName: name

  }
 
}

const tim = test("Timmy");

They say that " It should be noted that the class keyword declares a new function, to which a constructor is added. This constructor is invoked when new is called to create a new object"

I dont really understand what theyre saying here. It seems as if the constructor behaves similarly to function.

  **Your code so far**

// Only change code below this line
class Vegetable {
constructor (name) {
  return this.name = name;
}
}
// Only change code above this line

const carrot = new Vegetable('carrot');
console.log(carrot.name); // Should display 'carrot'
  **Your browser information:**

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

Challenge: Use class Syntax to Define a Constructor Function

Link to the challenge:

There doesn’t seem like much significance in those examples because right now neither one really does anything. It just returns an object with a single attribute. The value in a constructor is that it associates data with the instance of the class which can be used by methods: special functions associated with that class. Instead of a function that returns an object a single time, a class is a special kind of object that you can keep doing things with throughout your code.

So maybe you have a carrot and you create it like this

const carrot = new Vegetable('carrot').

Vegetables have other features besides a name. Maybe the Vegetable class has a method for calculating the calories in a specific vegetable

carrot.setCalories( 186, 0.25 ); // carrots have 186 calories per pound, this carrot weights 1/4 lb

And then we are trying to print the nutrition facts for a whole recipe:

const saladCalories = carrot.getCalories() + lettuce.getCalories() + tomato.getCalories() + cucumber.getCalories();

There are a bunch of built in features of JavaScript that you’ve used without realizing that this is how they work. Thing about Array.pop() or String.split().

We create classes when we know that we are going to want to create multiple things that have similar characteristics.

Hmm, I’m kind of understanding what you’re saying. So when we want other features, such as color or say even smell, are these other features done using setters and getters (the next challenge)?

Also, is the constructor, based solely on face value, the same as the function keyword, again in that they come before the parameter?

Getters and setters are a common class method, and usually what you’ll start out with, but class methods aren’t limited to getters and setters.

The real purpose of the constructor is the setup that should be done when an instance of the class is first created. So in the case of this very simple example, every vegetable needs a name, so the first thing that is done when the vegetable is created is storing the name on this. Constructors can be as simple or as complex as they need to be.

Oh I see, so when a constructor is invoked using new, it passes in the value we invoke into the parameter and runs the code inside the curly braces?

And I thought when a vegetable name is created, it would be stored in the variable carrot that we declared below the function. So what happens when we add a new vegetable name, does it get stored in this. or the variable it was declared in

btw thank you for your help! Sorry for not saying before I feel sort of dumb at the moment

1 Like

Looks like you’re getting it! When you start using some of this stuff you’ll find that you don’t have to think about it as hard. Try not to feel dumb! This isn’t intuitive at all. Keep asking good questions!

1 Like

Thank you! Some people here can be rude so I dont ask as many questions as I could. Thanks for your kind words!

1 Like

If people are being rude, please flag the post. If someone is harassing, please let us know. I mean, people disagree and there may even be some light teasing (that could be misunderstood), but this site is adamant that everyone feel welcome. It worries me that you want to ask questions but don’t because you’re concerned about people being rude.