Thursday, May 07, 2015

Javascript property - Prototype

Javascript has its own inheritance approach that uses the "prototype" property of the object or function.

So you can re-use the code and as well you can create your property using this inheritance approach.
Example,

I have one function called "Person" and it has 3 parameters such as first name,last name and telephone.

But in the future,I wanted to add some more parameters and use the Person function without modifying them.

In this case, you can create the new parameters and extend it the Person function.



 function Person(firstname,lastname,age,telephone)  
        {
            this.FirstName = firstname;
            this.LastName = lastname;
            this.Age = age;
            this.Telephone = telephone;
        }

        var itsMe = new Person("Murugesa", "Pandian", "37", "2232323");
        Person.prototype.Name = function () {
            return this.FirstName + " " + this.LastName;
        };
        Person.prototype.City = "Chennai";
        alert(itsMe.Name());
        alert("Property" + Person.prototype.City);