However the ‘Country_Italy’ object itself can be the property of other object:
We have created the Country object with the ‘Name’, ‘Capital’ and ‘Government’ properties and the ‘Government’ property is the object with own list of properties. We’ve also initialized these properties with the initial values. All is done in the one single string. The following construction creates and initializes the empty object:
The ‘Country’ function is the constructor function. Now to create the object using this defined constructor we use the new operator:
Just like in the previous example the property can be the object itself:
Government_France=new Government(“Jacques Chirac”);
In the above definition this operator provides an access to the object the described method belongs to.
The construction below fully repeats the above one:
calls the ‘describeCountry’ method of the ‘Country_Italy’ object.Prototypes based object oriented language.
Now let’s declare ‘Cn’ - the ‘Country’ object prototype:
Now we can call the ‘describeCountry’ method in any ‘Country’ descendant with only two strings of code:
Using this approach we can define in the prototype any number of the methods and properties. Defining methods and properties in the prototype rather then defining them directly for each object gives many advantages:
- You don’t have to define all required methods or properties any time the object is created. You define all the necessary methods and properties in the prototype and then all you have to do is to create the object.
- Makes your code more secure since you can keep all object definitions in the separate file
- Makes your code easier to readInheritance.
Constructor functions for these objects will look like these:
Take a look at the ‘Country’ object prototype.
The following construction defines ‘Country’ as ‘Government’ prototype (‘Government’ inherits the ‘Country’):
When ‘Government’ object is created it inherits all the ‘Country’ methods and properties. In other words, although ‘describeCountry()’ method is not defined for ‘Government’ object it is available since ‘Country’ object is defined as ‘Government’ prototype.
Government_France=new Government("Jacques Chirac");
New methods can also be defined for the ‘Government_France’ object:
Let’s complete the ‘Parliament’ object definitions.
Parliament_France=new Parliament("Jean-Louis Debre");
As you can see prototypes can also be modified for the inherited objects.
This very OOP approach was used in our company when we developed the CodeThat.Com’s set of the web controls. For example the ‘CodeThatCalendar’ is nothing but an object declared as:
Various methods of the control work with the layers giving the control fancy look and feel and enabling user perform various actions the calendar supposed to do. For example, the below function sets the value of the HTML form control to the date user have selected. This is performed each time user clicks the calendar date.
(The examples of CodeThatCalendar functions are not fully functional. They are listed here for illustrative purposes).