Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Constructor Functions and Prototypes | Prototypes, Inheritance, and the this Keyword
Objects and Prototypes in JavaScript

bookConstructor Functions and Prototypes

Constructor functions in JavaScript provide a way to create multiple objects that share the same structure and behavior. A constructor function is simply a regular function, but by convention, its name starts with a capital letter to indicate it is meant to be used with the new keyword. When you use new with a constructor function, JavaScript creates a new object, sets its internal prototype to the constructor's prototype property, and binds this inside the function to the new object. The prototype property of a constructor function is an object where you can define methods and properties that will be shared across all instances created by that constructor.

12345678910
// Constructor function for creating user objects function User(name, age) { this.name = name; this.age = age; } // Add a method to the User prototype User.prototype.sayHello = function() { console.log("Hello, my name is " + this.name + "."); };
copy

In the example above, the User constructor function sets the name and age properties for each new object. The sayHello method is added to User.prototype, which means all objects created with new User() will have access to sayHello through the prototype chain. When you create an instance using new User("Alice", 30), JavaScript sets up the object's internal prototype to reference User.prototype, so the instance inherits all prototype methods.

12345678910111213141516171819
// Constructor function for creating user objects function User(name, age) { this.name = name; this.age = age; } // Add a method to the User prototype User.prototype.sayHello = function() { console.log("Hello, my name is " + this.name + "."); }; const alice = new User("Alice", 30); const bob = new User("Bob", 25); alice.sayHello(); // Output: Hello, my name is Alice. bob.sayHello(); // Output: Hello, my name is Bob. // Both instances share the same sayHello function console.log(alice.sayHello === bob.sayHello); // Output: true
copy
question mark

What happens when you define a method on a constructor function's prototype in JavaScript?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

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

Awesome!

Completion rate improved to 7.69

bookConstructor Functions and Prototypes

Veeg om het menu te tonen

Constructor functions in JavaScript provide a way to create multiple objects that share the same structure and behavior. A constructor function is simply a regular function, but by convention, its name starts with a capital letter to indicate it is meant to be used with the new keyword. When you use new with a constructor function, JavaScript creates a new object, sets its internal prototype to the constructor's prototype property, and binds this inside the function to the new object. The prototype property of a constructor function is an object where you can define methods and properties that will be shared across all instances created by that constructor.

12345678910
// Constructor function for creating user objects function User(name, age) { this.name = name; this.age = age; } // Add a method to the User prototype User.prototype.sayHello = function() { console.log("Hello, my name is " + this.name + "."); };
copy

In the example above, the User constructor function sets the name and age properties for each new object. The sayHello method is added to User.prototype, which means all objects created with new User() will have access to sayHello through the prototype chain. When you create an instance using new User("Alice", 30), JavaScript sets up the object's internal prototype to reference User.prototype, so the instance inherits all prototype methods.

12345678910111213141516171819
// Constructor function for creating user objects function User(name, age) { this.name = name; this.age = age; } // Add a method to the User prototype User.prototype.sayHello = function() { console.log("Hello, my name is " + this.name + "."); }; const alice = new User("Alice", 30); const bob = new User("Bob", 25); alice.sayHello(); // Output: Hello, my name is Alice. bob.sayHello(); // Output: Hello, my name is Bob. // Both instances share the same sayHello function console.log(alice.sayHello === bob.sayHello); // Output: true
copy
question mark

What happens when you define a method on a constructor function's prototype in JavaScript?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
some-alt