Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www.austin.cc.tx.us/baldwin/

JavaScript Programming, Objects in JavaScript, an Overview

 
Java/Web Programming, Lecture Notes # 2135, Revised 05/25/98.

Preface

The material was prepared as part of a new tutorial that I plan to publish on Web programming in general.  It is not part of the Java academic curriculum at ACC.

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.

Introduction

Earlier lessons have guided you through the learning process for basic programming concepts such as data types, literals, variables, expressions, operators, arrays, and flow of control.  Although we occasionally encountered something with a distinct JavaScript flavor, much of what you learned is common with most other programming languages.

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.
 

Objects in JavaScript

According to Netscape:
 
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. 
 
This lesson will provide a very brief discuss of several important aspects of objects in JavaScript and then discuss those aspects in detail in subsequent lessons.
 

Properties of Objects

An object has state and behavior.  In JavaScript, the state of an object is stored in its properties.  Object properties in JavaScript are variables or other objects that are associated with the object.  JavaScript allows you to directly access the values of the properties of an object using the following syntax:
 
 
objectName.propertyName
.

Functions Revisited

Functions are one of the fundamental building blocks in JavaScript. An earlier lesson gave you a pretty good introduction to JavaScript functions.  We will let that suffice for now and revisit the topic of functions in a subsequent lesson devoted to functions.
 
 

Creating New Objects

There are several predefined object types in core JavaScript, such as an object for creating and manipulating arrays.  (The Array object was discussed in an earlier lesson.)

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-