Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 6.25

bookPrototype-Based OOP in JavaScript

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 1
some-alt