JavaScript Classes | Classes are templates for generating Objects



JavaScript Classes | Classes are templates for generating Objects

JavaScript Classes | Classes are templates for generating Objects

#javascript #javascriptclasses #javascriptobjects #javascriptexample

JavaScript Classes are templates for generating Objects.
Classes act as the blueprint or a template or a prototype for us to generate objects.
Sample:
Let’s say we are going to make a program that shows the information of all the players in the Turkish Super League.
Each player should have a name, number of goals, age, match played… etc. There should be a lot of features.
We cannot write these features haphazardly(randomly) for each individual player.
We need to make a sketch (template / draft / prototype / blueprint…)
In this sketch, we should write down all the characteristics that a player should have.
We do this job using Class.
class Player { …
And each player we generate using this Class is an Object.
const serdar = new Player( …
Serdar object, Arda object, Yunus object…
If we store these objects in an array [Serdar, Arda, Yunus…]
We can make a filter like
scorers.filter(scorer….
(Finding those who score more than 5 goals)
We can sort by goals scored.
scorers.sort((a,b)….
Each player has goals feature. Because each of them is generated using the template(the class) and there is a property called goals in this template(the class).
How would we do this if we didn’t use a template(the class)?
Or we can find the top scorer.
We can find the 11 most expensive…
If we pull data from an API in JSON format, it is presented to us in a similar way to the above format.
We can also use it in the same way.
Before presenting this API to the outside, they made templates with Classes and generated Objects using these Classes.
They did similar work before releasing the data.
We get this data presented to us as a string from the API (the data is transmitted as a string) and we convert it back to the above formats by typing JSON.parse(…
or
fetch(‘http://…
.then(response…
.then(data =…
The data we will use will be similar to what we described above.
And we can repeat the same operations that we applied above.