August 1, 2000 - Lazy Evaluation | WebReference

August 1, 2000 - Lazy Evaluation

Yehuda Shiran August 1, 2000
Lazy Evaluation
Tips: August 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

When implementing functional dependencies between properties, it is always a question when to trigger a recalculation following a change in one of the parameters. There are two opposite strategies. One says that you need to recalculate all parameters whenever one of the parameters or variables changes. The other one, sometimes called lazy evaluation, says that you need to compute a parameter only when you have to know its value. In such a methodology, a function is provided in case you want to force the evaluation.

In JavaScript, you can force a recomputation by calling the Document Object's recalc() method:

document.recalc(true);

In Column 65, Dynamic Properties, we showed how to program the Solar System. In this example, the need to update the location of the planet is implicit. None of the statements uses the top and left parameters of the planets, but the location of the planets in the solar system is determined by these parameters. Hence, to force the update of their location, we call the recalc() method every time interval:

function triggerRecalculation() {
  currentTime = new Date() - timeAt0;
  document.recalc(true);
}

For more on Dynamic Properties, go to Column 65, Dynamic Properties.