JavaScript Classes
JavaScript classes are where JavaScript kids go to learn… Not exactly, but it’s not exactly too far off. Classes are patterns or templates for objects. Blueprints, if you will, for something that will be filled later. A very common object that uses classes is for a website user. Imagine you know what information you want to store about each user, but you don’t have any users yet. You know you will want to know their last name, first name, email address, password, and whatever else you want to retain for later use. A class makes it easy to structure the desired data into an object. A class starts out with the word “class” followed by the name of the class starting with a capital letter, for example, User. This is to help us know if it is a class or not. The next part of the class is a “constructor”. This is the function inside the class that created the object from the predetermined data. Those items are passed into the function and used to fill in the object. So the keyword “constructor” is used followed by the passed items and then, what seems to be a list of redundant pieces of code. “this.lname = lname;” is an example of assigning the data into the object (W3Schools, 2015). “This” refers to the object being created at the time and then it’s back to the typical key-value pairs. And that’s all there is to create a class.
Get Ace D Baugh’s stories in your inbox
Join Medium for free to get updates from this writer.
Using them has huge benefits of streamlining data. A major use for this is when combined with database management where tables and schemas are predetermined. It makes code cleaner and easier to read, but it also has some downsides. Performance is a bit difficult to optimize. Programming is not always top to bottom, but classes make it harder to implement. It is also not recommended when working with React framework (Krasnov, 2020). Just to name a few.
References:
Krasnov, M. (2020, January 12). Please stop using classes in JavaScript. everyday.codes. Retrieved July 5, 2022, from https://everyday.codes/javascript/please-stop-using-classes-in-javascript/
JavaScript Classes. JavaScript classes. (n.d.). Retrieved July 5, 2022, from https://www.w3schools.com/js/js_classes.asp




