Nancy Cluts
Developer Technology Engineer
Microsoft Corporation
October 13, 1998
The following article was originally published in Site Builder Magazine (now known as MSDN Online Voices).
In Geek Speak Decoded #3, Client-Side and Server-Side Objects, you learned all about objects and what happens when you use them on both the server machine and on the user's (client) machine. With that information in hand, you may have even been able to create or buy some objects that you would like to use. You've got the software and the documentation, but now you're confronted with a whole new set of terms to understand.
Terms like event, function, method, property, and procedure.
This article will explain what these different terms mean when taken in the context of Internet development. You can get short definitions for most of these (and other) terms in the MSDN Online Glossary, but I'm going to go into a little more detail and provide some examples to help you along. As with all articles in the Geek Speak series, if you are already a seasoned developer, you already know this stuff, but if you're new to the development world or just new to Internet development, this article is for you.
In the real world, an event is something like opening night at the symphony or the World Series. In development terms, an event is a notification that something has happened to an object. The event may cause that object to do something. Examples of things that happen are mouse clicks, a key press, a window being loaded, or the focus changing from one object to another. When an event notification is sent to an object (i.e., the event fires), you have an opportunity to write some code to respond to that event. This code is also known as an event handler.
Event-driven programming is just what the name implies: code that responds to events. Events are central to Dynamic HTML (DHTML) programming. DHTML exposes these events (which means that you get notification when they happen) via the Dynamic HTML object model. In simple terms, an object model is the mechanism that allows you to program DHMTL. More information about object models can be found in the following articles:
Most events exposed via the DHTML object model begin with the word "on." Examples of events that DHTML supports are:
A full list of events exposed by the Dynamic HTML object model can be found in the documentation at http://www.microsoft.com/workshop/author/dhtml/reference/events.asp.
A property is a characteristic on an object that can be retrieved, set, or changed. Examples of properties include the color, font, and width of an object. You set a property on an object by referring to the object by name, then the property to set. The object and the property are separated by a period. The following code sets the identifier for a button named btn1 to 100. Objects, such as a button, have a "friendly" name (i.e., a name that is more intelligible than a series of numbers) and an identifier. The identifier is a unique number that identifies the button. You can refer to the object either by its name or its identifier. If you have three buttons on a page, you can determine which button was clicked by checking the button's ID or name.
btn1.id = 100
The property above was set by directly accessing the ID attribute. Some properties have properties, too. For example, if you want to change the color or font of a button, you would need to name the object, then specify that you want to change the style (the uber-property, if you will), and then specify the actual style that you want changed. For example, if you wanted to set the color of a button, btn1, to blue, you would use the following script:
btn1.style.color = blue
A full list of properties exposed by the Dynamic HTML object model can be found in the documentation at http://www.microsoft.com/workshop/author/dhtml/reference/properties.asp.
A method is a piece of code that an object calls to do something. Method is a term that is well-known in object-oriented programming (OOP) circles. In OOP, an object calls a method in response to some message that is sent to the object, in order to perform some task. Messages are the mechanism that is used in order for objects to communicate with each other. Microsoft's Windows operating systems rely heavily on this mechanism, sending messages to objects in order to notify the object that an event has occurred or that something should be done. Examples of methods supported by the window object are:
Methods are called via a mechanism similar to that for setting properties, with the exception that you can include extra data. You refer to the object by name and then to the method, and separate them by periods, and you include the extra data enclosed by parentheses. In the example below, the window object is opened using the URL sample.htm and its title displays "Sample Page." The window has a status bar, toolbar, and a menu bar, and it is 100 pixels high by 200 pixels wide.
window.open("sample.htm","Sample Page", "height=100,width=200,status=yes,toolbar=yes,menubar=yes,location=no");
A full list of methods exposed by the Dynamic HTML object model can be found in the MSDN Online Web Workshop DHTML Reference section.
A procedure is a piece of distinct code that performs some task. It is different from a method in that it is unrelated to objects. In other words, you cannot call a method unless you have created the object or unless the object is built in. In Visual Basic, a procedure is implemented as either a subroutine or a function. The difference is that a function returns a value, while a subroutine does not. For example, RetVal = foo() is a function, while foo() is a subroutine. JavaScript directly supports only functions. However, you can define functions that are associated with a JavaScript object, and then use the functions as you would a method.
Procedures are written by programmers in order to break down a large task into smaller, more managable pieces. They can be called over and over from different parts of an application and are valid within the scope of the process in which the application runs. You cannot call a procedure that is contained in another process unless that procedure has been exposed (via COM in Windows).
For example, let's say I have written an application that has a procedure that I have written called reallyCoolProc. I can call this procedure as much as I like, so long as it is within the scope of my application. I cannot, however, call this procedure from another application that I have written (the other application has no way of knowing where to find the code). If I want to use this procedure in other applications, I would package it up into a dynamic-link library (DLL) and use COM to expose the procedures I want to share.
When you write a procedure, you can pass data (known as parameters) to the procedure either by reference or by value. By value (in Visual Basic, byVal), means that you are passing the actual variable to the procedure. By reference, (in Visual Basic, byRef) means that you are passing a pointer (a reference) to the procedure.
An example of a subroutine, foo, that takes two parameters, param1 and param2, is:
foo(param1, param2);
An example of a function, foo, that takes two parameters, param1 and param2, and returns the value in a variable named retVal is:
retVal = foo(param1, param2);
Events, properties, and methods are all tools that you can use when working with objects. When an event fires, you can provide script, known as an event handler, to set or change properties of an object or to invoke a method. You can also write code to perform some task that is not necessarily related to an object. These procedures can return values (functions) or, if you are using VBScript, you can write procedures that don't return values (subroutines).
Ever since MSDN Online developer-technology writer Nancy Cluts became a godmother recently, she's been making us offers we can't refuse.