Javascript Prototype

function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; }

var myFather = new Person("John", "Doe", 50, "blue"); myFather.nationality = "English"; myFather; /** * Adding a property to a javascript object will add to the instance alone */ myFather.name = function () { return this.firstName + " " + this.lastName; };

myFather.name();

Person.nationality = "English";

var myMother = new Person("Sally", "Rally", 48, "green");

/** * Adding a property using prototype to a javascript main object will add to the Person Object which will be available for all instances of person.

function Person(first, last, age, eyecolor) { //.... name = function (){return this.firstName + " " + this.lastName; } } above is equal to below */ Person.prototype.name = function(){ return this.firstName + " " + this.lastName; }

myMother.name()