Message Sending

self

self is an important part of NewtonScript. It is a special variable that is available to a method while it is executing. Its value is always the frame to which the message was sent. Because of self, methods can have access to the frame that owns them. Look, for example, at the frame bankBalance and its three methods, Deposit, Withdraw, and Balance:

bankBalance := {
   total: 0,
   Deposit: func(amount) 
            begin
               self.total := self.total + amount;
            end,
   Withdraw:func(amount)
            begin
               self.total := self.total - amount;
            end,
   Balance: func() 
               return self.total,
};

To send messages to these various methods requires no more than

bankBalance:Deposit(50);
bankBalance:Withdraw(20);
bankBalance:Balance();
30
As each of the three methods executes, self is the bankBalance frame; it is the frame to which each message is sent. Thus, self.total is the total in the bankBalance frame.


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

Last modified: 1 DEC 1996