Thursday, 10 April 2014

Object Oriented Javascript

Hello,

Let’s see the implementation of object oriented programming in JavaScript :

      function students(FName, LName, CityName) { 
            this.City = CityName
            this.FirstName = FName;
            this.LastName = LName;
            this.GetName = function () {
                return this.FirstName + " " + this.LastName;
            }
        }
       
        // This method is also child method of "students" Method
        students.prototype.ChangeName = function (FName, LName) {
            this.FirstName = FName;
            this.LastName = LName;
        }

        function Check() {
            var stud = new students("Ajay", "Patel", "kalol");
            var GetFullName = stud.GetName();
            alert(GetFullName + " FROM " + stud.City);
            stud.ChangeName("A", "P");
            GetFullName = stud.GetName();
            alert(GetFullName + " FROM " + stud.City);


            // You will get other result for new object of Method
            var stud1 = new students("Hitesh", "Patel", "kalol");
            var GetFullName = stud1.GetName();
            alert(GetFullName + " FROM " + stud1.City);
            stud1.ChangeName("H", "P");
            GetFullName = stud1.GetName();
            alert(GetFullName + " FROM " + stud1.City); 

        }

No comments:

Post a comment