ES6 - Use class Syntax to Define a Constructor Function

Tell us what’s happening:
Describe your issue in detail here.

The problem I have is I looked deeper into each type of object generators and there are already 3 types.

  1. Factory - not requiring -this- and -new-.
  2. Constructor - requiring -this- and -new- (optional?). Good for making many new objects?
  3. Class - It’s just a constructor with syntactic sugar? No, apparently not just syntactic sugar. There are much more. They are more strict and requiring -new- when call. They act in block as well.

Am I understanding right? I don’t kow the lesson just jump into this without anymore elaboration.

Would you be able to clarify in the simplest terms the differences and their advantages for me abit more?

Your code so far

// Only change code below this line

// 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/104.0.0.0 Safari/537.36

Challenge: ES6 - Use class Syntax to Define a Constructor Function

Link to the challenge:

I could do a long explanation … or I could point you here.

Thankyou, unfortunately I don’t have access to that page. I’m sure it’s what I’m looking for. …is long explanation possible? xD pleassee…

If you sign up for medium, you can get an extra free article for the month.

Or you can do some more googling. Learning to “search and find the answer” will serve you better than learning to “ask someone to explain it to me”. On the job, you will have to do A LOT of googling. The habit of asking people to explain things to you will not server you well.

Do some research. If you get stuck, then check back. At work, they’ll respect if you’ve done some research before depending on others. And you will learn so much more, so much better.

ah it’s OK, I download an extension to bypass the limit for free.

I mean, I spent the whole day looking through syntax for each, the purpose, and the advantage for each lesson that definitely jumped way too far without covering such an important topic. It gives the wrong impression that there’s only a class function that is needed.

I highly doubt that many people actually bother to notice, to begin with.

People ask around all the time as “research”, otherwise there won’t be that many posts on StackOverflow. I was hoping there are the better or simpler explanation.

I just mean that as you go on, you’ll do much better searching for either a definitive source (if it exists) or for a variety of opinions. One of the most important skills a dev can have is knowing how to lookup answers.

People ask around all the time as “research”, otherwise there won’t be that many posts on StackOverflow.

Yeah, and they sometimes get reamed for that. And they’re just hurting themselves - they’d do much better to learn how to fish rather than asking around for someone to give them a fish.

Every answer on stack overflow started with asking question. They are not on the job, they are using the resources available on the internet. The difference between factory functions and a class is not immediately apparent, and it’s certainly not unreasonable to ask for someone to clarify it. A paywall is a paywall. I don’t think using this forum for (checks notes) “exactly the purpose it was intended for” is deserved of a canned “do your own research” response. You are suggesting that a person use the internet and do some googling, when that is in fact exactly what they are doing. IF you personally do not wish to participate in that process, maybe just don’t.

Every answer on stack overflow started with asking question.

Yes, and a lot of them have already been answered 1000 times. And a lot of them could be answered with a simple google search and a little reading. If I google “javascript difference class and object constructor”, I get 7.4M hits. I’m willing to bet that many of those are relevant and that most of those are not behind a paywall.

I still think that is a better skill - searching - than getting someone to explain it to you. If you’re at this point in the curriculum, you would do yourself a favor in fostering some independence.

Other people were welcome to answer. I said that if a sufficient answer wasn’t found to check back. My goal here is to help people become strong developers. I still believe my actions were inline with that goal.

I think the questions might need some clarification as well.

  1. Yes, I’d say you are responsible for returning the object in that case.

  2. Yes and no. A constructor function can be called without new but then it doesn’t work the same way. There are for example built-in JS constructor functions you can call with or without new. They do not return the same thing.

typeof new Date()
'object'

typeof Date()
'string'
  1. A class is considered syntactic sugar. But I’m not sure what it is you are comparing it against exactly. For one, you can’t call it without new so as per #2 they are not the same. Hoisting also doesn’t work the same.
const person = new Person('John'); // Cannot access 'Person' before initialization

class Person {
  constructor(name) {
      this.name = name;
  }
  sayHello() {
      return `Hello, I'm ${this.name}`;
  }
}

const person = Person('John'); // Class constructor Person cannot be invoked without 'new'

Anyway, I would agree that the questions do call for research in any case.

There’s a guy that is explaining why the class is syntactic sugar on the surface, but not so much if going deeper into the background.
It’s way out of my beginner skills atm. But if people insist that it is, I’ll take that as a yes for now.

The problem being, that you’ve assumed they haven’t bothered to look it up at all, when after their first response to you it was clear they had done some of their own research. The article you linked was very helpful. I had a hard time finding any clear explanation of the difference between the two anywhere outside of that article. The problem isn’t that there aren’t articles that explain it. The problem is that many strong programmers have a hard time communicating what they already understand to people that don’t. Most things I can muddle through, but sometimes discourse is required to bridge that gap. I have to thank you for that resource you’ve offered. Yes using google is an important and powerful skill. So is asking for help when that fails, as has happened here. Your response, in light of that, is rather discouraging. That’s my 2 cents.

1 Like

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