Java Reference
In-Depth Information
Note: Don't Repeat Yourself
Don't Repeat Yourself,
or DRY, is a principle of programming that specifies
that every part of a program should only be written once. This avoids du-
plication and means that there's no need to keep multiple pieces of code up
to date and in sync.
If you have assigned a function to a variable, you need to place parentheses after the vari-
able to invoke it as a function:
goodbye();
<< "Goodbye World!"
Remember: you need parentheses to invoke a function―either by name or by reference to
the variable it is assigned to. If you skip the parentheses, you are simply referencing the
function itself rather than invoking it, as you can see here:
goodbye;
<< function bye(){
alert("Goodbye World!");
}
All that has been returned is the function definition that the variable
goodbye
is pointing
to, rather than running the code. This can be useful if you want to assign the function to
another variable, like so:
seeya = goodbye;
<< function bye(){
alert("Goodbye World!");
}
Now the variable
seeya
also points to the function called
bye
and can be used to invoke
it:
seeya();
<< "Goodbye World!"
The result can be seen in
Figure 4.2
.
