Constructor 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 + "."); };
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
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 7.69
Constructor Functions and Prototypes
Glissez pour afficher le menu
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 + "."); };
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
Merci pour vos commentaires !