Re: Scope of globals, parents and children, "load script" and all that jazz
Re: Scope of globals, parents and children, "load script" and all that jazz
- Subject: Re: Scope of globals, parents and children, "load script" and all that jazz
- From: Timothy Bates <email@hidden>
- Date: Thu, 17 May 2001 20:42:40 +1000
On 17/5/01 1:21 PM, "Sander Tekelenburg" <email@hidden> wrote:
>
I separated some stuff into a main script (parent) and a
>
separate script library (child) that is called by the main script with the
>
"load script" command.
>
The original ('all-in-one')
>
script uses a lot of globals (17).
a good clued that it was time to build a new object/script library
>
why did I need to:
>
1) declare those same globals again, at the top of the script
>
2) declare them yet again within each handler! (well, only those globals that
>
apply to that particluar handler)
First off, you don't need the original declarations any more: the data are
stored in the object, so having them in the main handler is redundant. If
their values are needed in the main object, access them through the object
pPropertyName of oObject, or Object's pPropertyName
Second off, if they are properties, they will be available within the
handlers of your object. You don't need to declare them again, vis
if your object contained these lines:
property pVar1:"hello Teklenburg"
to sayHi()
return pVar1
end
then
tell object to sayHi()
--> "hello Teklenburg"
>
I thought the loaded script would 'see' the
>
Parent's globals
absolutely not. that would be a disaster. think about what happens if an
object reuses the name of a var in your script without your knowing ...
think "objects are like people, I find out what they believe by asking them
questions, not by reading their minds"
>
apparently both the parent and the child must be 'manually'
>
informed of each other's globals. Why? And is there a more elegant way to do
>
this?
the key here is to think "if I have to tell the object about this variable,
maybe it should be a property of that object, and not of my script?"
>
I've looked through the chapters in the AppleScript Language Guide that seem
>
relevant, but it isn't getting any clearer for me.
that is why opacity and object oriented both start with the same letter in
the dictionary ;-)
>
(In the long run I will probably split the library up into several libraries.
>
And I would like to get rid of as many globals as I can. But for now I was
>
just trying if I could easily switch to this construction without having to
>
get rid of all globals just yet.)
no need to split the library just for the sake of it, but, yes - often a bit
of thought leads to this. I just created a graphics library rather than
extend my screen library. Thinking this stuff through leads to great things
which you will look back on with pride.
tim
Dr Timothy Bates <
mailto:email@hidden>
Macquarie Centre for Cognitive Science (MACCS)
Macquarie University
Ph 61 (2) 9850 8623
Fx 61 (2) 9850 6059