Re: difference between property and variable
Re: difference between property and variable
- Subject: Re: difference between property and variable
- From: deivy petrescu <email@hidden>
- Date: Mon, 10 Dec 2007 21:31:06 -0500
On Dec 10, 2007, at 17:13, has wrote:
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).
Has did you really try to help Justin with your answer?
Up to properties Has is absolutely right. From properties on lets go
with the definitions below.
First, lets not talk about script objects. In AS after local
variables you have global variables and properties.
Global as the name implies are defined globally, but, and it is a big
but, you have to declare that a handler will use that global.
Also, in AS, using SEditor, any variable declared at the top of the
script is, by default, global.
So:
//---script 1---//
set x to 1
on teste()
set x to x+1
end
teste()
--> error x is not defined
//---end---//
//---script 2---//
set x to 1
on teste()
global x
set x to x+1
end
teste()
--> 2
//---end---//
Globals persist after runs, therefore, when you declare a variable and
fill it up with "garbage", depending on the size of the "garbage", it
can cause some damage. This means that
//---script 3---//
set x to read a large files
--
do stuff with x
--
set x to ""
//---end---//
Is a very good idea.
Property is just like global. The difference is all handlers inherit
the properties without any other input.
So:
//---script 4---//
property x: 1
on teste()
set x to x+1
end
teste()
--> 2
//---end---//
Another difference, properties do not have to be "set" initially.
At the end of scripts 2 and 4 the value of x is 2. However, next time
you run script 2, you "reset" x to 1.
In script 4 you do not. Running it twice will return 3.
Local variables are variables defined inside a handler, you do not
have to declare them as local, they are local by default.
I think of one exception for this rule.
As for what Has said about globals, it would be true if you run Smile
and use AS Terminal to declare (set) your variable. Those variables
are global variables in the true sense of global, as Has put it, good
for the whole program, or session as I would prefer.
Deivy
_______________________________________________
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