JavaScript Classes and Objects
Introduction to Classes in JavaScript
Classes in JavaScript are a syntactic sugar over prototypes and constructor functions. They provide a cleaner and more readable way to create objects and implement inheritance, but under the hood, they use the same prototype-based structure.
Defining a Class
class User {
total = 55; // Class field
constructor(username, email) {
this.username = username;
this.email = email;
}
print() {
console.log(this);
}
}
const u1 = new User("death", "dc@dc.com");
u1.print();
Explanation:
total
: A public class field shared by all instances.constructor
: Initializes instance-specific properties.print
: A method shared via the prototype.
Inheritance Using Classes
class Programmer extends User {
constructor(username, email, lang) {
super(username, email); // Call the parent class constructor
this.lang = lang;
}
print() {
console.log("PROGRAMMER");
super.print(); // Call the parent class method
}
hello() {
console.log("Programmer Hello");
}
}
const p1 = new Programmer("death", "dc@dc.com", "JS");
p1.print();
Explanation:
extends
: Establishes inheritance from theUser
class.super
: Calls the parent class's constructor or methods.- Overridden
print
: Demonstrates polymorphism by modifying behavior.
Deep Inheritance
class Dev extends Programmer {
constructor(username, email, lang) {
super(username, email, lang);
}
}
const dev1 = new Dev("code", "code@dc.com", "Python");
console.log(dev1);
Explanation: The Dev
class inherits from Programmer
, which in turn inherits from User
. This demonstrates multi-level inheritance.
Behind the Scenes: Prototypes
Classes in JavaScript are built on prototypes. When you define a class method, it is added to the prototype of the class, and all instances share the same method.
Object.prototype.hello = () => {
console.log("hello");
};
p1.hello(); // hello
Explanation: Extending the Object
prototype makes the method available to all objects, but this is generally discouraged as it can lead to conflicts.
Key Features of Classes
- Syntactic sugar over prototypes and constructor functions.
- Supports inheritance using
extends
andsuper
. - Encapsulation with class fields and methods.
- Polymorphism through method overriding.
Best Practices
- Use classes for better readability and maintainability.
- Avoid modifying built-in prototypes like
Object.prototype
. - Leverage
super
for clean inheritance.
Playlist of JS-Journey (JavaScript)
1. Introduction to JavaScript - What, Why, and How? | JavaScript Journey | In Hindi | Death CodeWatch
2. Variables & Comments in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
3. Understanding Data Types in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
4. Master Type Conversion & Reference Data Types π | JavaScript Journey | In Hindi | Death CodeWatch
5. Mastering Operators in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
6. Mastering Numbers & Math Functions π | JavaScript Journey | In Hindi | Death CodeWatch
7. Strings and String Methods in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
8. Mastering Dates and Date Methods in JavaScript β³ | JavaScript Journey | In Hindi | Death CodeWatch
9. JavaScript Arrays & Array Methods Explained β³ | JavaScript Journey | In Hindi | Death CodeWatch
10. Objects and Object Methods Explained π | JavaScript Journey | In Hindi | Death CodeWatch
11. Conditional Statements | If, Switch & Ternary Operator | JavaScript Journey | In Hindi | Death CodeWatch
12. Mastering Loops (for, while, do-while) | JavaScript Journey | In Hindi | Death CodeWatch
13. Understanding Functions | JavaScript Journey | In Hindi | Death CodeWatch
14. HOFs, Callback Functions, Arrow Functions & More! π | JavaScript Journey | In Hindi | Death CodeWatch
15. Mastering Scope & Hoisting Explained in Hindi π₯ | JavaScript Journey | In Hindi | Death CodeWatch
16. Map, Filter, Reduce & forEach Explainedπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
17. Understanding this Keyword, Function Context & IIFEπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
18. Dive into Call Stack & JavaScript Execution Contextπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
19. Master Async Await & Event Loop π₯ | JavaScript Journey | In Hindi | Death CodeWatch
20. Promises in JavaScript Made Easy π | JavaScript Journey | In Hindi | Death CodeWatch
21. Fetch Function in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
22. JavaScript OOP Basics π | JavaScript Journey | In Hindi | Death CodeWatch
23. Mastering Prototypes & Inheritance in JS π | JavaScript Journey | In Hindi | Death CodeWatch
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeCurrently Playing
25. Mastering Getter and Setter (Why & How to use them?) π | JavaScript Journey | In Hindi | Death CodeWatch
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeWatch
27. DOM Basics π | JavaScript Journey | In Hindi | Death CodeWatch
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeWatch
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch