DEV Community

Discussion on: How To: Build a Linked List in JavaScript

Collapse
 
efpage profile image
Eckehard • Edited

You should mention two benefits from using classes too:

1. Encapsulation

If you implement some methods to add, delete or serialize your nodes, the user does not need to know about the details of your list. Writing a program he can just use these methods.

If you decide to change your internal structures, you will need to change this methods to. But the program does not need to be changed. So, your code changes are only local to the class definition and your have to change your code only once. This is far less prone to side effects.

2. Inheritance

Now you have designed your class with a pointer to the data. Assume, you need a list that has different properties, let say you want to store an image. You can easily derive a new class that inherits all your methods (including add, delete and serialization). You will have to change some parts of your class to work with the new data type, but lot of the functionallity will probably need no change.

Maybe, you decide to add more capabilities to your current class sometime in the future. The derived class for images will inherit this new methods too. So you probably will save a lot of time as you do not have to add this new methods to the child classes.

And: As the classes are one family, objects are related too. So you can write a program that deals with members of this family regardless of the type.

Thank you very much for your post! ItΒ΄s good to have more practical examples of this kind.

Collapse
 
am20dipi profile image
Adriana DiPietro

Yes! I think I may have a follow-up post and these are great points to consider. Thanks.