JavaScript classes and objects explained with practical examples

    JavaScript code editor showing object oriented patterns
    JavaScript code editor showing object oriented patterns

    Object oriented programming in JavaScript looks intimidating when you first meet words like class, prototype, inheritance, or constructor, but this lecture breaks them into small, practical steps that you can actually use in real projects.

    Instead of treating classes as a purely theoretical topic, the instructor keeps jumping between the slides and the browser console, showing how each concept behaves when you run it in a real file and refresh the page.

    Starting from plain objects and the meaning of this

    The lecture starts with the familiar idea of a simple object literal, built with curly braces and stored in a constant, for example a student object with properties like fullName and marks, plus a function that prints the marks.

    Loading code...

    From there the instructor introduces the this keyword inside the method and explains it in a very grounded way: inside an object method, this refers to that specific object, so this.marks is similar to writing student.marks while keeping the code reusable.

    The idea is reinforced live in the console by changing the object, calling the method again, and watching how this always points to the current object instance, not to some global value you cannot see.

    Prototypes and where built in methods come from

    Once the basic object feels comfortable, the lecture moves to prototypes, starting from a very simple question: if we never wrote a toString method or array push function, how does JavaScript still let us call them?

    By expanding objects and arrays in the console, the instructor shows the hidden prototype property, itself another object, that carries common methods like toString for plain objects and push, pop or length for arrays.

    Loading code...

    She then builds a custom employee object and uses the __proto__ property to link a new object, such as karanArjun, to the employee prototype so the new object can reuse the calculateTax method without duplicating code.

    Loading code...

    An important nuance is covered here as well: if both the object and its prototype define a method with the same name, JavaScript picks the one on the object first, which becomes the basis for the later discussion on method overriding.

    From templates to real classes and the new keyword

    After prototypes, the lecture transitions to modern class syntax, describing a class as a template or blueprint that you use when you need many similar objects, such as multiple cars or employees with the same structure but different values.

    Loading code...

    The instructor explains that the new keyword is responsible for creating a fresh object from the class blueprint, attaching the class methods to its internal prototype, and then returning the ready to use instance.

    This section also clarifies how classes avoid repetitive code, since you define shared behaviour once on the class and then instantiate as many concrete objects as your application needs.

    Constructors, properties and passing data in

    Next, the lecture introduces the constructor method, described as a special function inside the class that runs automatically whenever you create a new instance, making it perfect for initial setup and assigning properties.

    Loading code...

    By passing arguments like brand and mileage into the constructor, each object gets its own data while still using the same class code, and the console logs make the order of constructor calls very clear.

    The video also points out that if you do not write a constructor yourself, JavaScript silently creates a default one, but writing your own gives you control over required parameters and initial values.

    Inheritance, method overriding and the super keyword

    With classes and constructors in place, the lecture moves into inheritance, using examples like a general Person class and more specific Engineer and Doctor classes that share common behaviour but add their own methods.

    Loading code...

    The extends keyword tells JavaScript that Engineer should inherit properties and methods from Person, so an engineer object can both eat and work without copy pasting the shared methods.

    A key point here is method overriding: when both the parent and child classes define a method with the same name, the child version is used, which the instructor demonstrates by giving Person a generic work method and then redefining work in Engineer.

    The super keyword is introduced in two contexts, first to call the parent constructor so you can safely use this inside the child, and second to call a parent method from inside an overridden method when you want to keep the original behaviour and then add more steps.

    Loading code...

    Building a small user admin model with classes

    To pull these ideas together, the lecture walks through a mini project scenario for a college website where you model users, students, teachers, and admins using classes and inheritance instead of scattered objects.

    Loading code...

    In this design, regular users can only view shared data, while the admin class, derived from User, gains an extra editData method that can change the underlying data variable, reflecting real permission layers in web apps.

    The example hints at how you could grow this pattern into deeper hierarchies, with separate Student and Teacher classes extending User and then adding methods related to updating marks or addresses while still sharing login related information.

    Quick look at error handling with try catch

    At the end, the instructor briefly shifts to error handling to show what happens when your code throws an exception and how it can stop the rest of the script if you ignore it.

    Loading code...

    By wrapping the risky line in a try block and catching the error object, you can log the problem while letting the rest of the script continue, which matters a lot once you build larger interfaces or work with external APIs.

    The segment is short, but it connects nicely with the rest of the lecture because real applications that use classes for structure also need defensive code paths to avoid one unexpected bug breaking all remaining logic.

    Love this story? Explore more trending news on javascript

    Share this story

    Frequently Asked Questions

    Q: Why should I use classes instead of plain objects in JavaScript?

    Classes help when you need many similar objects, such as multiple users or cars, because you define the shared behaviour once and then create new instances without repeating code.

    Q: What is the practical role of the constructor in a JavaScript class?

    The constructor runs automatically when you create a new instance and is the ideal place to set required properties like name, email, brand or mileage.

    Q: How does inheritance reduce duplication in real projects?

    You put common properties and methods in a parent class, then extend it for more specific roles like Engineer, Doctor or Admin, so each child class reuses the shared logic and only adds extra behaviour.

    Q: When should I use the super keyword in JavaScript classes?

    Use super inside a child constructor to call the parent constructor before using this, and inside overridden methods when you want to run the parent version and then add more steps.

    Q: Why does the lecture include try catch along with classes and objects?

    Because real applications that rely on classes also need safe error handling, and try catch prevents one failing line from stopping the rest of your script.

    Read More

    No related articles found matching this topic.