Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Prototype-Based OOP in JavaScript | ES6 Class Fundamentals
JavaScript Classes and OOP Foundations

bookPrototype-Based OOP in JavaScript

JavaScript uses a prototype-based approach to object-oriented programming, which means that objects can inherit properties and methods directly from other objects. Every JavaScript object has an internal link to another object called its prototype. When you try to access a property or method on an object, JavaScript first looks for that property on the object itself. If it does not find it, it looks up the prototype chain, searching each prototype in turn until it finds the property or reaches the end of the chain.

This system allows you to share methods and properties efficiently between objects. Rather than duplicating functions for every object instance, you can attach methods to a constructor function's prototype. All objects created by that constructor will then have access to those shared methods via the prototype chain.

12345678910111213141516
// Constructor function for creating Person objects function Person(name) { this.name = name; } // Add a method to the prototype Person.prototype.greet = function() { return "Hello, my name is " + this.name; }; // Create two instances const alice = new Person("Alice"); const bob = new Person("Bob"); console.log(alice.greet()); // Output: Hello, my name is Alice console.log(bob.greet()); // Output: Hello, my name is Bob
copy
question mark

Which statement best describes how prototype-based inheritance works in JavaScript?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain how the prototype chain works in more detail?

What happens if I add a new method to the prototype after creating instances?

How is this different from class-based inheritance in other languages?

Awesome!

Completion rate improved to 6.25

bookPrototype-Based OOP in JavaScript

Veeg om het menu te tonen

JavaScript uses a prototype-based approach to object-oriented programming, which means that objects can inherit properties and methods directly from other objects. Every JavaScript object has an internal link to another object called its prototype. When you try to access a property or method on an object, JavaScript first looks for that property on the object itself. If it does not find it, it looks up the prototype chain, searching each prototype in turn until it finds the property or reaches the end of the chain.

This system allows you to share methods and properties efficiently between objects. Rather than duplicating functions for every object instance, you can attach methods to a constructor function's prototype. All objects created by that constructor will then have access to those shared methods via the prototype chain.

12345678910111213141516
// Constructor function for creating Person objects function Person(name) { this.name = name; } // Add a method to the prototype Person.prototype.greet = function() { return "Hello, my name is " + this.name; }; // Create two instances const alice = new Person("Alice"); const bob = new Person("Bob"); console.log(alice.greet()); // Output: Hello, my name is Alice console.log(bob.greet()); // Output: Hello, my name is Bob
copy
question mark

Which statement best describes how prototype-based inheritance works in JavaScript?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 1
some-alt