0
What is prototype in Javascript
How to use prototype in javascript and what is the defference between Constructor and Prototype.
2 ответов
+ 1
var arr = [1,2,3];
Suppose you give arr[-1]to acess the array then computer go to the prototype and search for -1 and following operation would be done on that method if there is no prototype in your code then it will show undefined.
0
In JavaScript, a prototype is like a template or a blueprint for objects. It allows you to create new objects with shared properties and methods. Here's a straightforward example:
// Create a prototype object
const personPrototype = {
greet() {
console.log('Hello!');
}
};
// Create a new person object based on the prototype
const person1 = Object.create(personPrototype);
person1.greet(); // Output: "Hello!"
// You can also add unique properties to each object
person1.name = 'Alice';
const person2 = Object.create(personPrototype);
person2.name = 'Bob';
console.log(person1.name); // "Alice"
console.log(person2.name); // "Bob"