+ 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 !🐟
5 Respuestas
+ 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 🐟.
+ 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
+ 7
Airree objects are instances of classes and not the other way round.
0
CLASSES WHERE WE PUT METHODS... EXPLORE W3SCHOOL