🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

Understanding JavaScript's Prototypal Inheritance - A Dev's Guide

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Hey fellow devs! After years of working with PHP's class-based inheritance, diving into JavaScript's prototypal inheritance felt like learning to write with my left hand. Today, I want to share what I've learned about this unique approach to inheritance that makes JavaScript special.






The Basics - What Makes It Different?



Unlike PHP or Java where we work with classes, JavaScript uses prototypes. Every object in JavaScript has an internal link to another object called its "prototype". Think of it as a fallback mechanism - when you try to access a property that doesn't exist in an object, JavaScript looks for it in the object's prototype.




CODE
const pet = {
makeSound() {
return "Some generic sound";
}
};

const cat = {
purr() {
return "Purrrr";
}
};

// Set pet as the prototype of cat
Object.setPrototypeOf(cat, pet);

// Now cat can use methods from pet
console.log(cat.makeSound()); // "Some generic sound"
console.log(cat.purr()); // "Purrrr"









The Proto Chain - It Goes Deeper



Here's where it gets interesting. Prototypes can have their own prototypes, forming what we call the "prototype chain". JavaScript will keep looking up this chain until it finds what it needs or hits a null prototype.




CODE
const animal = {
eat() {
return "Nom nom nom";
}
};

const pet = {
makeSound() {
return "Some generic sound";
}
};

const cat = {
purr() {
return "Purrrr";
}
};

Object.setPrototypeOf(pet, animal);
Object.setPrototypeOf(cat, pet);

// cat can now access methods from both pet and animal
console.log(cat.purr()); // "Purrrr"
console.log(cat.makeSound()); // "Some generic sound"
console.log(cat.eat()); // "Nom nom nom"









The Constructor Pattern - A More Familiar Approach



If you're coming from a class-based language like PHP, you might find the constructor pattern more familiar:




CODE
function Animal(name) {
this.name = name;
}

Animal.prototype.eat = function() {
return `${this.name} is eating`;
};

function Cat(name) {
Animal.call(this, name);
}

// Set up inheritance
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.constructor = Cat;

Cat.prototype.purr = function() {
return `${this.name} says purrrr`;
};

const felix = new Cat("Felix");
console.log(felix.eat()); // "Felix is eating"
console.log(felix.purr()); // "Felix says purrrr"









Modern JavaScript - Classes Under the Hood



ES6 introduced the class syntax, which might look familiar to PHP developers. But don't be fooled - it's just syntactic sugar over prototypal inheritance:




CODE
class Animal {
constructor(name) {
this.name = name;
}

eat() {
return `${this.name} is eating`;
}
}

class Cat extends Animal {
purr() {
return `${this.name} says purrrr`;
}
}

const felix = new Cat("Felix");









Pro Tips from the Trenches



After years of working with both PHP and JavaScript, here are some tips I've learned:




  1. Prefer composition over inheritance when possible

  2. Keep prototype chains shallow - deep chains can hurt performance

  3. Use class syntax for cleaner code, but understand prototypes for debugging

  4. Always set the constructor property when creating inheritance chains manually






Wrapping Up



Understanding prototypal inheritance might feel strange at first, especially if you're coming from PHP or Java. But once it clicks, you'll appreciate its flexibility and power. It's one of those JavaScript features that makes you think differently about object-oriented programming.



Have you encountered any interesting challenges with JavaScript inheritance? Drop a comment below - I'd love to hear your stories!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding JavaScript's Prototypal Inheritance - A Dev's Guide

Thematisch verwandte Begriffe: Understanding, JavaScripts, Prototypal, Inheritance · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...