In this JavaScript Tutorial, we will learn how to create object called Person. Then we will add data and function members to it. Finally, we will see how to use the Person object and call its member function.
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 45 46 47 48 49 50 |
<!DOCTYPE HTML> <HTML> <HEAD> <TITLE>Learn Java Script</TITLE> </HEAD> <BODY> <script type="text/javascript" > //Sample 01: Allocate memory for the class var Person1 = new Object(); //Sample 02: Assign Data Members Person1.FName = "Abdul"; Person1.MName = "K"; Person1.LName = "Kadhar"; //Sample 03: Assign Members Function Person1.getName = function(){ return this.FName + " " + this.MName + ", " + this.LName; } //Sample 04: Let us create Two More Persons var Person2 = new Object(); Person2.FName = "John"; Person2.MName = "S"; Person2.LName = "Kevin"; Person2.getName = function(){ return this.FName + " " + this.MName + ", " + this.LName; } var Person3 = new Object(); Person3.FName = "Abdul"; Person3.MName = "K"; Person3.LName = "Kadhar"; Person3.getName = function(){ return this.FName + " " + this.MName + ", " + this.LName; } //Sample 05: Keeps this Objects in a Array var PersonArray = [Person1, Person2]; PersonArray[2] = Person3; //Sample 06. Invoke the Member Function document.write("Person 2 Formatted Name: " + PersonArray[1].getName() + "</br>"); </script> </BODY> </HTML> |
- Arrays Add, Remove, Join via push, pop, shift, unshift, concat #17
- Creating Object with Function Template #19
Categories: JavaScript-Tube