php javascript jquery html css mysql

Object Oriented JavaScript tutorial

A more comprehensive example


function newPerson(){ 
    
      this.Person = {
         
           arms:2,
           legs:2,
           head:1,
           torso:1,
           alive:true,
           age:0,
           height:18,
           name:null,
           fines:function(){
           
               this.fine={
                 parking:0,
                 library:0,
                 disorderly:0,
                 setParkingFine:function(parkingFine){
                    this.parking = this.parking + parkingFine;
                 },                 
                 setLibraryFine:function(libraryFine) {
                    this.library = this.library + libraryFine;
                 },
                 setDisorderlyFine:function(disorderlyFine) {
                    this.disorderly = this.disorderly + disorderlyFine;
                 },
                 getFines:function(){
                   
                    allFines = new Object();
                    
                    allFines.parking = this.parking;
                    allFines.library = this.library;
                    allFines.disorderly = this.disorderly;
                    allFines.total = this.parking + this.library + this.disorderly;
                    
                    return allFines;
                    
                 }
               }
           
               
           } ,
           
           agePerson:function(){
             this.age = this.age++;
           
           },
           getAge:function(){
              return this.age;
           },
           
           setAge:function(age){
               this.age = age;
           },
           
           killPerson:function(){
             this.alive = false;
           },
           
           getStatus:function(){
              var stringThing = "";
              if(this.alive === false){
                  stringThing = "this person is dead."
              } else{
                 stringThing = "this person is alive and well."
              }
              return stringThing;
           },
           
           setName:function(name){
              this.name = name;
           },
           
           getName:function(){
               if(this.name != null){
                  return this.name;
              }else{
                return "This person has no name";
              }
           }
      }
 
}
   
     John = new newPerson();          //create an object called John
     console.log(John) ;              //send the output to Firefox console
     John.Person.setName("John");     //set the name of John to "John"
     console.log(John.Person.getName());    //retrieve the name using method getName
     John.Person.fines = new John.Person.fines(); //create a new fines object
     John.Person.fines.fine.setParkingFine(120) ; //set fine for parking to 1200
     John.Person.fines.fine.setLibraryFine(12) ; //set fine for library to 12
     John.Person.fines.fine.setLibraryFine(6) ; //add 6 to library fine
     Sally = new newPerson();                   //create Sally Object
     Sally.Person.setName("Sally");             //set Sally object name to "Sally"
     console.log(Sally);                        //take a peep at Sally
     console.log(John);                         //take a peep at John
     Sally.Person.killPerson();                 //bye bye Sally
     //     set johnsFines to the object of the method getFines
     var johnsFines = John.Person.fines.fine.getFines();  
     console.log(johnsFines);                   // see the fines...ouch!
     
Code snippet by John Green 

Next we will look at this script a little more closely.

<< Previous Page | Next Page > >

Article by J C Green

This article was written for non-profit educational purposes. Comments? Suggestions? Rants or raves? E-mail me using the form on this website.