Prototype | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Prototype

What are prototype in js... Can someone explain with examples

3rd Sep 2019, 7:01 PM
Amirabbas
Amirabbas - avatar
3 Answers
+ 2
A prototype is (exactly the prototype of objects) the basic set of methods and properties of an object. An Arrays prototype method split() can be called by any variable of type String. var str = new String("test"); var arr = str.split('e'); A variable of type string inherits every property and method from It's type. String.prototype.split -> str.split
3rd Sep 2019, 7:17 PM
Coding Agent
Coding Agent - avatar
0
Coding Agent So like i have something like this function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; } This is an Object constructor So the Person inherits the methods and propertys from Person.prototype that itself inherits from Object.prototype Yeah?
4th Sep 2019, 12:14 PM
Amirabbas
Amirabbas - avatar
0
Yes, then you create an object with var myPerson = new Person(params); And it inherits the properties of Person.
5th Sep 2019, 5:22 PM
Coding Agent
Coding Agent - avatar