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.
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.
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"
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.
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"
If you're coming from a class-based language like PHP, you might find the constructor pattern more familiar:
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"
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:
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");
After years of working with both PHP and JavaScript, here are some tips I've learned:
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!
Atas ialah kandungan terperinci Memahami Warisan Prototaip JavaScript - Panduan Pembangun. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!