Portability
March 15, 1999
While writing some modular code, you realize that you
really need a module which whips up a perfect chocolate
mousse -- light and airy, yet with a strong hit of chocolate
cut against the tang of a coffee subflavor. Problem
is, you don't really know how to make the perfect mousse --
but why re-invent the wheel? You ask around and find
that someone else has already coded a module called
airyMousse() and it does just what you need.
A portable module is modular, to be sure, but it goes one
step further -- it operates in a world unto itself
without relying on conditions or data existent in other
modules within the whole program. A portable module may
or may not require some incoming data to work with and it
may or may not return some outgoing data at the end;
but within its own machinations it keeps to itself.
A main technique in creating portable modules is to use
local variables. A local variable is a value
label which is only valid within the given function. It
has no life outside the function and does not interfere
with any variables of the same name outside the function.
In JavaScript you use the var statement to declare
a local variable within a function, or you create local
variables with incoming data in the function definition.
function airyMousse
(eggs,sugar,cream,coffee,chocolate)
{ var base=eggs*sugar;
var liquids=(coffee/2)+(chocolate*3);
var base2=base+cream;
whip(base);
whip(base2);
return base+base2+liquids;
}
The function airyMousse() accepts several incoming
parameters -- the data passed by the calling statement
is assigned to the local variables specified in the function
definition: eggs, sugar, cream, coffee, and
chocolate. Within the function itself several local
variables are declared for working with the data: base,
base2, and liquids. Because these are local to
this function these variables are unknown to any other
parts of the program and do not affect any variables of
the same name elsewhere. In this way, this function is
portable because it could be used in virtually any program
without needing to know about its other parts.
Modularity
Creating Portable and Modular Client-Side Scripts
Embedding Code
|