Class and Methods | Bondar Academy
Course: JavaScript for Testers
Module: JavaScript Fundamentals
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explored the concept of classes and methods in JavaScript, which help organize code into reusable components, similar to functions. Creating a Class A new class is created using the class keyword followed by the class name in PascalCase . For example: class CustomerDetails {} Defining Methods Inside the class, methods can be defined with the following syntax: methodName() {} For instance, to create a method called printFirstName : printFirstName(firstName) { console.log(firstName); } Using the Class To use the class methods outside of the file, you need to: Export the class. Import the class in another file using import . Create an instance of the class with new . Example of creating an instance: const customerDetails = new CustomerDetails(); Calling Methods After creating an instance, you can call methods like so: customerDetails.printFirstName('Steve'); Refactoring and Documentation For cleaner code, you can refactor by exporting an instance directly. Additionally, you can add annotations for methods using: /** * Description of the method * @param {string} lastName */ This enhances code readability and provides useful information in IDEs. Summary Classes and methods in JavaScript provide a way to abstract and organize code. Use the class keyword, define methods, and manage instances effectively to improve code structure.