+ 14

What are JavaScript classes for?

Hello everyone ! I finished the JavaScript tutorial, but I did not understand the usefulness of classes. I'd like to have sample codes with classes to help me understand how they work. Thank you !🐟

1st Jul 2019, 7:31 PM
Mr.Fish
Mr.Fish - avatar
5 Answers
+ 47
Classes are like LEGO parts. They are standard, they can be similar like siblings but have distinctive differences. You can build pretty much anything with them. For example you create an online store with thousands of items for sale. Usually all those diverse items have common properties: name, description, photo, price and so on. Instead of writing code for every single product you can create a big class (a blueprint) that fits them all. Individual properties of products can be assigned later with a few lines of code. A video games benefit from using classes. You can have one enemy class but numerous enemies in your game. You can find video tutorials (and code samples) on the topics above on YouTube or continue with strait scripting. It's OK in JavaScript 🐟.
2nd Jul 2019, 10:31 PM
đŸ‡ș🇩 Drake CDR đŸ‡ș🇩
đŸ‡ș🇩 Drake CDR đŸ‡ș🇩 - avatar
+ 11
Thank you very much Vitya, Sonic and Airree
3rd Jul 2019, 9:14 PM
Mr.Fish
Mr.Fish - avatar
+ 7
Classes are instances of objects. It is useful, when you need the same type of multiple objects. Here's an example (and a shameless self plug): https://code.sololearn.com/W5TB2sgNmboO/?ref=app In this code, it would be kind of hard to make hundreds of balls without classes, so it is very useful. Maybe you could use Object.create, but classes are probably better
1st Jul 2019, 8:00 PM
Airree
Airree - avatar
+ 7
Airree objects are instances of classes and not the other way round.
2nd Jul 2019, 2:46 AM
Sonic
Sonic - avatar
0
CLASSES WHERE WE PUT METHODS... EXPLORE W3SCHOOL
3rd Jul 2019, 4:29 AM
Joshua Martin Fontanilla
Joshua Martin Fontanilla - avatar