JavaScript Prototypes and Prototype Inheritance
What are Prototypes?
In JavaScript, every object has an internal link to another object called its prototype. Prototypes allow objects to inherit properties and methods from other objects.
Example: Adding Methods Using Prototypes
function CreateTroop(name, target) {
this.name = name;
this.target = target;
}
CreateTroop.prototype.attack = function() {
console.log(`${this.name} is Attacking`);
};
const barb = new CreateTroop("barbarian", "ground");
const archer = new CreateTroop("archer", "both");
barb.attack(); // barbarian is Attacking
archer.attack(); // archer is Attacking
Explanation:
CreateTroop
: A constructor function.CreateTroop.prototype.attack
: Adds a method to the prototype, shared by all instances.barb
andarcher
: Instances ofCreateTroop
that inherit theattack
method.
Prototype Inheritance
Prototype inheritance allows one constructor function to inherit properties and methods from another constructor function.
function User(name) {
this.name = name;
this.total = 56;
this.print = function() {
console.log(this);
};
}
function Teacher(name, subject) {
User.call(this, name); // Call User constructor
this.subject = subject;
}
const t = new Teacher("DeathCode", "JS");
console.log(t);
t.print(); // Logs the Teacher instance
Explanation:
User.call(this, name)
: Inherits properties from theUser
constructor.Teacher
: A constructor function extendingUser
.t
: An instance ofTeacher
, with properties from bothTeacher
andUser
.
Extending Built-in Prototypes
String.prototype.removeUnderscore = function() {
return this.replaceAll('_', ' ');
};
let name = "Hello_Death_Code";
console.log(name.removeUnderscore()); // Hello Death Code
Explanation: Adds a custom method removeUnderscore
to the String
prototype.
Adding Methods to All Objects
Object.prototype.deathcode = function() {
console.log("Hello Dosto, Subscribe krdo");
};
"".deathcode(); // Hello Dosto, Subscribe krdo
Explanation: Adds a method deathcode
to the Object
prototype, making it available to all objects.
Best Practices
- Use prototypes to share methods between instances efficiently.
- Be cautious when extending built-in prototypes to avoid conflicts.
- Use
Object.create
or classes for more structured 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 CodeCurrently Playing
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeWatch
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