Research ProtoJS - mikehov/Dating-app GitHub Wiki

Prototype Javascript Research

Mike Hovenier | Tech-4

What is Prototype in Javascript

In Javascript its possible to adjust or add propeties of a object.

function Student() {
    this.name = 'John';
    this.gender = 'Male';
}

var studObj1 = new Student();
studObj1.age = 15;
alert(studObj1.age); // 15

var studObj2 = new Student();
alert(studObj2.age); // undefined

As you can see in the code, studObj1 will get 15 but studObj2 won't cause there is no value given. So if you want to add something that will for multiple uses, you can use something they call Prototyping. Every function includes prototype object by default. Prototype makes it so you properties can be attached to it which will be shared across all the instances of its constructor function.

So you type something like this:

function Student() {
    this.name = 'John';
    this.gender = 'M';
}

Student.prototype.age = 15;

var studObj1 = new Student();
alert(studObj1.age); // 15

var studObj2 = new Student();
alert(studObj2.age); // 15

Now because of Student.prototype.age = 15;, they both wil alert "15". If you will remove just .prototype, you answer will be undefined twice.

How can I use Prototype in Javascript for my project?

To structure my code more instead of repeating it.

Source

Fun Fun Function. (2016, January 25). Prototypes in JavaScript - FunFunFunction #16 [Video file]. YouTube. Retrieved from https://www.youtube.com/watch?v=riDVvXZ_Kb4

The Coding Train. (2017, February 22). 9.19: Prototypes in Javascript - p5.js Tutorial [Video file]. YouTube. Retrieved from https://www.youtube.com/watch?v=hS_WqkyUah8

TutorialsTeachers. (n.d.). Prototype in JavaScript. Retrieved May 29, 2020, from https://www.tutorialsteacher.com/javascript/prototype-in-javascript