In this JavaScript Tutorial, we will use Function Template technique to create Person object. Later, we will access the object to call its member functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<!DOCTYPE HTML> <HTML> <HEAD> <TITLE>Learn Java Script</TITLE> </HEAD> <BODY> <script type="text/javascript"> //Sample 01: Create Function Template function Person(FName, MName, LName) { this.FName = FName; this.MName = MName; this.LName = LName; this.getFullName = function() { return FName + " " + MName + " " + LName; } this.getNamePart = function(part) { switch (part) { case "F": return FName; break; case "M": return MName; break; case "L": return LName; break; } } } //Sample 02: Create Two Persons var person1 = new Person("Arun", "T", "Narayan"); var person2 = new Person("Boris", "K", "Becker"); //Sample 03: Print Persons document.write(person1.getFullName() + "<br/>"); document.write(person2.getNamePart("F") + "<br/>"); document.write(person2.getNamePart("M") + "<br/>"); </script> </BODY> </HTML> |
Categories: JavaScript-Tube