Re: difference between property and variable
Re: difference between property and variable
- Subject: Re: difference between property and variable
- From: has <email@hidden>
- Date: Mon, 10 Dec 2007 22:13:31 +0000
Justin Laden wrote:
Currently I am reading over Matt's O'Reilly book, and I could use a
better understanding of what separates a Property from a Variable.
Locals, properties and globals are all kinds of variables. What
differentiates them is scope [1]:
- A local variable's scope (i.e. lifetime and where it can be accessed
from) is limited to the handler in which it's defined: only code in
the handler can use that variable, and once the handler code finishes
running that variable ceases to exist.
- A property's scope is limited to the script object in which it's
defined. e.g. If you define a property in your main script, that
property will cease to exist when the script finishes running and
likewise ceases to exist. (Properties are equivalent to module
variables and instance variables in other languages.)
- A global variable has the widest scope of all, extending throughout
an entire program regardless of where it's defined.
Unless you use additional script objects to define modules or for
object-oriented programming (and most folk don't), the difference
between properties and globals is basically a non-issue for you.
Properties do have the additional benefit in that they allow you to
define an initial value at compile time, which is generally convenient
and just good coding practice [2], so as a rule of thumb I'd recommend
sticking to properties for storing values that need to be accessible
throughout a script, locals for anything else, and ignore globals
completely.
HTH
has
[1] And warts, but I won't go into all of those here, as it's better
to get a clear, logical understanding of the important concepts in
place before you have to muck it up again learning about all the
practical flaws in AppleScript's implementation.
[2] Globals are undefined until you assign values to them, and
attempting to access a global before a value is assigned to it will
result in a error. They're also an open invitation for hard-to-spot
bugs if you do do use modules/OOP in your programs. IOW, globals are a
design mis-feature that can and should be avoided, since locals and
properties will do everything you need if you write good code (and if
they don't do everything you need, that's a sign that you're writing
bad code and should address that accordingly).
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden