1function Person(name) {
2 this.name = name;
3}
4Person.prototype.getName = function() {
5 return this.name;
6}
7
8var person = new Person("John Doe");
9person.getName() //"John Doe"
1function Person(first, last, age, eye) {
2this.firstName = first;
3this.lastName = last;
4this.age = age;
5this.eyeColor = eye;
6}
7
8Person.prototype.nationality = "English";
9
10var myFather = new Person("John", "Doe", 50, "blue");
11console.log("The nationality of my father is " + myFather.nationality)
1/*Prototype is used to add properties/methods to a
2constructor function as in example below */
3
4function ConstructorFunction(name){
5 this.name = name //referencing to current executing object
6}
7ConstructorFunction.prototype.age = 18
8let objectName = new ConstructorFunction("Bob")
9console.log(objectName.age) //18
1//simply prototype means superclass
2
3function Student(name, age) {
4 this.name = name,
5 this.age = age;
6}
7
8var stu1 = new Student("Wasi", 20);
9var stu2 = new Student("Haseeb", 25);
10//this class is add in all of the objects
11Student.prototype.class = "Free Code Camp";
12
13console.log(stu1);
14console.log(stu2);
15
16
1// function prototype
2void add(int, int);
3
4int main() {
5 // calling the function before declaration.
6 add(5, 3);
7 return 0;
8}
9
10// function definition
11void add(int a, int b) {
12 cout << (a + b);
13}