If you like this post then please subscribe to my full feed RSS. You can also subscribe by Email. Not sure how this works?

The Object-Oriented Programming was one of the biggest breakthroughs programmers ever made. It's a different approach to programming, which will make your life a lot easier when you deal with long programs. You don't use the ASP because you want to write "Hello, world!" on a single web page. Instead, you will use it to create complex web sites, such as pages for electronic commerce, or polls and quizzes, or in order to manipulate a complex database. In these cases, you'll have to deal with many items and operations, and countless repetitions become boring and tiresome. Instead of writing the same code lines over and over again, you can now use the object-oriented programming.

This is not a new program, but rather, a new way of thinking. If you've spent some time studying HTML and DHTML, you're probably used to it, but if you've never met this concept, you should spend some time to make sure you understand it correctly.

Let's take one very simple example. Let's say you have a car, which, of course, is an object. Now, this car has some properties that differentiate it from other cars, like the number of doors, the speed, the color and so on. Besides, it also performs a number of actions: it accelerates, decelerates, stops. Well, in a programmer's language, the car is called "object", the properties are called, well "properties" or "variables" and the functions and actions are called "methods". And that's all there is to it.

Some objects are built-in, but others you'll need to define for yourself. In JScript, the built-in objects include string, date, math, and array. We will discuss them in detail in due time.

Methods are those operations that modify the object, by manipulating the variables. In our example, you manipulate the "speed" variable of the car object by using the acceleration and deceleration methods. Only an object's methods should modify its variables, you cannot do it directly. If you need to change the variable "color", for instance, you must have a method, called "painting", that will help you do that.

You will find a full list of JScript objects and their properties and methods in the JScript reference, available on the net. The general syntax is:

object.property_name object.method_name()

The String object is the one that works with text. One of its properties is called length, and it returns the number of characters of the respective string. One of its methods is called substring(), and it subtracts a string, contained between the specified values. For instance, substring(5,12) will return a string formed by all the characters between the 5th and the 12th character of the original string.

var str = "This is a demo script. You can modify it as much as you like. Copyright softwareprojects 2004″

document.write(str.length) // This will return the value 93

document.write(str.substring(0, 23)) // This will return the string This is a demo script.

document.write(str.toUpperCase()) // This will return the string THIS IS A DEMO SCRIPT

document.write(str.toLowerCase()) // This will return the string this is a demo script

Use these examples in your script and see what happens. As you can see, the names of the objects, their properties and their methods are quite easy to figure out - you don't need a lot of schooling to understand what a method called "toUpperCase()" does. Note that this method does not require any parameters - there is noting more to define for it, but the brackets are still needed.

Now that we've figured this one out, let's move on to the date object. You can assign the date to a variable, much in the same way as you assign a number or a string:

var x = new Date()

You can write down any parameters you want between brackets:

var x = new Date("January 1, 2004″)

var x = new Date("January 1, 2004 12:00:00″)

If you don't write any parameters, the script will simply use the date from the respective machine. The two main methods for this object are getDate(), which returns the date from an objects, and, for our variable x, looks like this:

x.getDate()

and setDate(), which sets the date of the month in an object, and looks like this:

x.setDate()

There are various different variations of the two, such as getDay(), getHours(), getMinutes(), setMonths(), setSeconds(). I think you got the idea. Experiment with the Date object a little. It's easy, simple and you see it used very often, on many web pages. This is the perfect object to help you get used to the syntax and the use of parameters.

The Math object deals with the basic mathematical constants and functions. Same as the rest of the objects discussed in this chapter, the Math object is already there, you don't need to create it, and you may find it very useful on occasions. It has functions such as PI, which returns the PI, naturally, and E, which returns the base of a natural logarithm. (If you can't remember what the natural base of a logarithm is, then give it a try and see if you can figure it out.) The methods are pretty neat, if you want to make some mathematical games, you have abs(x), which returns the absolute value of x, cox(x) - the cosine of x, round(x), which rounds x to the nearest integer, and random(), which returns a random number between 0 and 1.

Did You Enjoy This Page? These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • MisterWong
  • BlinkList
  • Furl
  • Netscape
  • Spurl
  • StumbleUpon
If you like this post then please subscribe to my full feed RSS. You can also subscribe by Email. Not sure how this works?

Leave a Reply