Java Reference
In-Depth Information
Here are two nearly identical functions, the only difference being that
moveTo-
Bound()
is preceded with the
bound
keyword, whereas
moveToUnBound()
is not.
This subtle difference does affect how the variables
cell1
and
cell2
are evalu-
ated. First, a change in the value of the arguments (
r
and
c
set to 5) causes both
functions to be re-invoked (twice) resulting in new and updated
cell1
and
cell2
instances. However, when the value of
translate
is changed, the behavior of
the bound and unbound functions diverge. The
moveToUnBound()
function is
unaware of any change to the
translate
variable and is consequently not re-
invoked, whereas
moveToBound()
is re-invoked because the bound function can
detect the change in
translate
. Here's the output of this script:
cell1: row=0, col=0
cell2: row=0, col=0
cell1: row=5, col=5
cell2: row=5, col=5
cell1: row=5, col=5
cell2: row=12, col=12
An important point regarding bound functions is that the function body is no dif-
ferent than the previously discussed
bound block expression
(with all of its limi-
tations). The last expression—typically the only expression—inside the function
body is the bound function's return value. Finally, bound functions may be
invoked outside the context of a bind expression. Calling a bound function in this
way is no different than calling a regular, plain old function.
JavaFX includes a mechanism that facilitates the catching and handling of data
modification events. By adding a
trigger
to a variable, you associate a block of code
that will be executed every time the variable is modified. A trigger is formally intro-
duced to a variable declaration by appending a phrase starting with the keywords
on
replace
. Although strongly discouraged, a trigger, in its most rudimentary form,
can be used to mimic the behavior of
bind
. For example, the following statements:
var x : String;
var y = bind x;
can be re-written using triggers in a nearly equivalent manner:
var x : String on replace {
y = x;
}
var y : String;
Search WWH ::
Custom Search