One of the very useful (and cool) features of compiled JavaFX Script will be closures. In a nutshell, JavaFX Script closures provide the ability to define a function within another function with the inner function having access to the local variables of the outer function. This feature is enabled by the fact that in compiled JavaFX Script, functions are first-class objects, which provides the ability to assign functions to variables and to pass functions as arguments to other functions. You can read more about closures in this Wikipedia article.
Closures are often used to implement callback functions, many times handling UI events. Since the UI part of compiled JavaFX Script isn't ready for prime-time yet, I'd like to show you a callback example in which a function is called when a timer expires. You can run this example by downloading and building the JavaFX Compiler in its current state from the Subversion repository in the OpenJFX Compiler Project. Here's the source code for the example:
/*
* ClosureExample.fx - Demonstrates closures, and that functions are first-class
* objects in compiled JavaFX Script.
*
* Uses JavaFX Script compiled version
* Developed 2007 by James L. Weaver (jim.weaver at lat-inc dot com)
*/
import java.lang.System;
import java.lang.Thread;
public class ClosureExample {
function startTimer(millis:Integer, wakeUp:function():Void):Void {
Thread.currentThread().sleep(millis);
wakeUp();
}
function setAlarm(sleepTime:Integer):Void {
var message = "Time to wake up!";
var wakeUpFunc =
function() {
System.out.println(message);
System.out.println("You slept for {sleepTime} milliseconds");
};
startTimer(sleepTime, wakeUpFunc);
}
}
var example =
ClosureExample {
};
example.setAlarm(3000);
Walking Through the Closure Related Features of this Example
After creating an instance of the ClosureExample class, and calling its setAlarm() function, you'll notice that an anonymous function is defined and assigned to a variable named wakeUpFunc. After that, the startTimer() function is invoked, passing the desired duration of the timer, as well as the wakeUpFunc reference.
Within the startTimer() function, the sleep() method of the Java Thread class is called, which causes the current thread to sleep for the desired number of milliseconds. This, by the way, demonstrates JavaFX Script's ability to leverage the power of Java classes (see the Putting My CTO Hat On post). After the thread wakes up, the callback function that was passed into the startTimer() function is invoked, which performs the closure.
The net effect is that the callback function accesses and prints the values of local variables of the setAlarm() function. The closure feature allows access to these variables from within the scope of the callback function, even though they're not local to the callback function.
Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download available at the book's Apress site
i like this part of the blog:"After creating an instance of the ClosureExample class, and calling its setAlarm() function, you'll notice that an anonymous function is defined and assigned to a variable named wakeUpFunc. After that, the startTimer() function is invoked, passing the desired duration of the timer, as well as the wakeUpFunc reference." is very good
Posted by: buy viagra | April 26, 2010 at 04:02 PM