What is prototype in Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is prototype in Javascript

How to use prototype in javascript and what is the defference between Constructor and Prototype.

12th Aug 2020, 8:09 AM
Rohani
Rohani - avatar
2 Answers
+ 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.
12th Aug 2020, 11:41 AM
Akhil Repalle
Akhil Repalle - avatar
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"
29th Sep 2023, 12:13 PM
Chandan Kumar
Chandan Kumar - avatar