NewtonScript Examples

A Counter Object

Now let's create a frame that contains two slots. One holds the current count; the other is a function that increments the count. A QuickTime movie of this example is available.

counter := {
    lastCount: 0,
    Next: func()
    begin
        return lastCount := lastCount + 1;
    end,
}
#4408F31  {lastCount: 0, 
           next: <function, 0 arg(s) #4408D6D>}
We test the code by sending the frame the Next message repeatedly:

counter:Next();
#4        1
counter:Next();
#8        2
counter:Next();
#C        3
Now, let's print out the counter object to verify that the value of the lastCount slot has indeed changed:

counter
#4408F31  {lastCount: 3, 
           next: <function, 0 arg(s) #4408D6D>}
Resetting the counter back to 0 is no harder than making its value zero:

counter.lastCount := 0;
#0        0
Now, when you send the Next message, you get brand new counts:

counter:Next();
#4        1
counter:Next();
#8        2
counter:Next();
#C        3

An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996