Video thumbnail for JavaScript Class Inheritance _ Static And Private Methods In JavaScript

JavaScript Class Inheritance _ Static And Private Methods In JavaScript

Aug 29, 2023
Class inheritance is a JavaScript feature that allows you to create new classes that are based on existing classes. This can be used to reuse code and to create more complex and powerful classes. Static methods are methods that are associated with a class, not with an instance of a class. Static methods can be called without creating an instance of the class. Private methods are methods that are only accessible from within the class that defines them. Private methods cannot be called from outside the class. The following code defines a class called Person with a static method called getNumberOfPeople(): class Person { static getNumberOfPeople() { return 0; } } The getNumberOfPeople() method is a static method. It can be called without creating an instance of the Person class. The following code defines a class called Employee that inherits from the Person class: class Employee extends Person { constructor(name, age) { super(name, age); } } The Employee class inherits from the Person class. This means that the Employee class has all of the properties and methods of the Person class, including the getNumberOfPeople() method. The following code defines a private method called _calculateSalary(): class Employee { constructor(name, age) { super(name, age); } _calculateSalary() { // This method is private and cannot be called from outside the class } } The _calculateSalary() method is a private method. It cannot be called from outside the class. I hope this summary is helpful. Please let me know if you have any other questions. Here are some additional things to keep in mind about class inheritance and static and private methods in JavaScript: Static methods are often used to store constants and to perform operations that do not depend on an instance of the class. Private methods are often used to implement the implementation details of a class and to hide those details from the outside world. Class inheritance and static and private methods are powerful tools that can be used to make your JavaScript code more concise, readable, and efficient. I encourage you to learn more about them and use them in your own JavaScript programs.
Programming