Building a CMS - Object Oriented Code
(Page 4 of 5 )
Each document feature is going to need its own unique markup, so let's write a Javascipt object for each feature that will contain what we need for that feature, along with a method that will validate the form entry, and assemble the markup from the information gathered from the form.
First, a little background on Javascript objects for those of you who have never coded one. Javascript has some object oriented features which can, just as in any other language that has them, be used to make code more portable and modular. In the near future Javascript is expected to go fully object oriented, complete with class structure, inheritance and strong typing. Until then we have what are called object constructors that look like functions but are used as objects in our code.
These objects can have properties which are set when creating an instance of the object or left in their initialized state until they are set later. They also have public methods which are defined outside of the code block that defines the object but are declared as methods of the object by using the keyword "this."
function myObject(myProperty)
{
//property set via the argument sent to the object
this.objectProperty = myProperty;
this.initializedProperty = false;
//establish myMethod as a method of this object
this.myMethod = myMethod;
}
function myMethod()//method
{
if(this.myProperty > 0)
{
this.initializedProperty = true;
}
}
Once you have this code, all you need to do to use it is to create an instance of it. As in many programming languages that have object orientation you use the keyword "new."
//create an instance
var newObj = new myObject(2);
//invoke the instance's myMethod method
newObj.myMethod();
alert(newObj.initializedProperty);
If you executed this code, you would get an alert box that would display the word "true."
Next: Constructing the Main Heading Markup >>
More Web Hosting How-Tos Articles
More By Chris Root