Monday, May 22, 2017

#1 2005-10-13 03:13:34 pm

julifos
Administrator
From: MalasaƱa, Madrid
Registered: 2002-11-20
Posts: 2014

What is "variable scope"?

The scope of a variable is the place where it is relevant and accessible. For example, the scope of properties (a kind of variable) is "all its script object", and the scope of local variables is only "the handler where it resides".

As this is a vague concept, let's see it with a live sample:

Applescript:

set foo to "dummy variable"
display dialog foo
something()

on something()
   display dialog foo
end something

Here, the variable called "foo" is a local variable. So, if you run this code, you will see a dialog saying "dummy variable". Later, the handler "something" will throw an error, since it can't access the variable "foo" (because "foo" was not declared in its own "scope").

However, if you declare the variable "foo" as a global or property, its scope will include the entire script object, and it will be accessible from any place within such script object:

Applescript:

property foo : "dummy variable"
display dialog foo
something()

on something()
   display dialog foo
end something

Here you will receive two "dummy variable" dialogs, since the scope of "foo" is available to every part of the script (both "something" and the main script, which is an implicit "run" handler).

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.033 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)