0

i saw a code where people used prototype keyword in javascript but i do not understand what is the importance of this keyword and when to use it.

here is some code related to javascript prototype

1st example

function Person(fn,ln)
{
    this.FirstName = fn;
    this.LastName = ln;
}

Person.prototype.GetFullName = function()
{
    return this.FirstName + ' ' + this.LastName;
}

var p = new Person('jon','smith');
alert(p.GetFullName()); 

2nd example

function Person(fn,ln)
{

    this.FirstName = fn;
    this.LastName = ln;
    Person.prototype.GetFullName = function()
    {
        return this.FirstName + ' ' + this.LastName;
    }
}

var p = new Person('jon','smith');
alert(p.GetFullName());

1) just see the code what prototype keyword will do here?

2) also see two same code in 1st example prototype used outside of Person function and in second example prototype is wrapped in person function. looking for good explanation with more sample example to understand it properly . thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • Did you read [this question](http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work?rq=1)? Or [this one](http://stackoverflow.com/questions/4841175/trying-to-understand-the-point-of-prototypes-in-javascript?rq=1)? Or [this one](http://stackoverflow.com/questions/23327954/javascript-oop-prototype-outside-of-constructor?rq=1)? – Pointy Jul 10 '14 at 14:18
  • Never assign to the `.prototype` inside the constructor! – Bergi Jul 10 '14 at 15:34

0 Answers0