Differences between properties and global variables

(Alberto González Ramírez) #1

What are the differences between properties and global variables?
I do not refer about how they are declared and assigned a value or the ability to “remember” values between runnings of a script.
Is there a “rule” to use properties in stead of global variables?
Or is it a matter of taste?

Thanks in advance.

(Mark Alldritt) #2

From a technical perspective, properties and global variables are in all ways the same thing. They only differ in how they are declared.

However, there are subtle scoping issues when using global variables. Because global variables come into existence at the moment they are assigned a value, references to a global within a handler can be interpreted as a local variable if the declaration of the global happens later in the source code (lexically).

I recommend declaring all global variables at the head of your script using a global statement or assign each global variable an initial value. This allows you to inform AppleScript of the variables scope.