The purpose of this series of lessons is to teach you how to program in JavaScript. The goal is to provide a programming tutorial that will be accessible to persons with no programming experience and will be equally useful to persons who already have programming experience using JavaScript or some other language.
Now we are about to depart from that world. Much of what you learn from this point forward will be very specific to JavaScript. Although JavaScript and other languages make use of objects, the manner in which this is accomplished is very different between JavaScript and the most of the rest of the object-oriented programming world.
Under the assumption that many of you might want to go forward from
JavaScript and learn to program using other languages such as Java, I may
occasionally provide some discussion about how JavaScript differs from
the other major object-oriented programming languages.
JavaScript is based on a simple object-oriented paradigm. An object is a construct with properties that are JavaScript variables or other objects. An object also has functions associated with it that are known as the object's methods. In addition to objects that are predefined in the Navigator client and the server, you can define your own objects. |
objectName.propertyName |
There are other predefined objects that are specific to a Navigator document such as an object for manipulating the history list maintained by Navigator.
In addition, JavaScript lets you define and create your own new objects. This is an area where JavaScript departs significantly from most of the rest of the world of object-oriented programming.
If you want to be able to create multiple instances of the new object type, you can define a constructor function and then invoke that function using the new operator to create any number of new objects. This method of defining new object types and creating new objects has some degree of similarity with techniques used in other OOP languages.
On the other hand, in JavaScript 1.2, if you need only one instance of the new object type, you can create it using an Object Initializer that, to my knowledge, has no counterpart in either Java or C++. In this case, you don't need to define a constructor function. In Navigator 4.0, you can also use an initializer to create an array (a capability which we did not discuss in the earlier lesson on arrays).
A really major difference between JavaScript and other OOP languages such as Java and C++ is that in JavaScript, you can always add a property to a previously defined object. You can do this in such a way that it impacts only a single object, or you can do it in such a way that it impacts all objects of that type.
A method is a function associated with an object. You define a method the same way you define a standard function. Then you use a special syntax to associate the function with an existing object. You can then call the method in the context of that object.
If you want the method to apply to all objects of a given type, you can define methods for an object type by including a method definition in the object constructor function.
For the Java and C++ programmers in the crowd, JavaScript does give
us one familiar handle to hang onto. Like Java and C++, JavaScript
users this to refer to the current object. In general,
this refers to the calling object when a method is called
on a specific object.
-end-