Info
Open the page on your phone

Tell us about a prototype in JavaScript?

In JavaScript, every function has a prototype property that is used to attach properties and methods that should be inherited by objects created from that function. When an object is created from a constructor function using the 'new' keyword, it inherits the properties and methods from the constructor function's prototype.

Here is an example of a simple prototype in JavaScript: 

                        
function Person(name) 
{
    this.name = name;
} 

Person.prototype.greet = function() {
    return 'Hello, my name is ' + this.name;
};

var person1 = new Person('Alice');
console.log(person1.greet()); // Output: Hello, my name is Alice