Prototype in JavaScript:-
All the JavaScript objects has an object and its property called prototype & it is used to add and the custom functions and property. See Following example in which we create a property and function.
var empInstance = new employee();
empInstance.deportment = "Information Technology";
empInstance.listemployee = function(){
}
All JavaScript objects inherit properties and methods from a prototype.
Ex:-
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
var myFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sally", "Rally", 48, "green");
document.getElementById("demo").innerHTML =
"My father is " + myFather.age + ". My mother is " + myMother.age;
</script>
</body>
</html>
Output:-
JavaScript Objects
My father is 50. My mother is 48