Re: Parents/Child Scripts
Re: Parents/Child Scripts
- Subject: Re: Parents/Child Scripts
- From: has <email@hidden>
- Date: Fri, 10 May 2002 23:03:18 +0100
Steve Suranie wrote:
>
Can anyone send me a short example of how to pass a variable between a
>
parent and child script.
Not sure what you mean here: "parent" and "child" are terms used in object
oriented programming - specifically in discussions about inheritance.
A child object inherits the properties and methods (handlers) of the
parent. You don't "pass" values from one to the other as such. Rather, the
child - in addition to its own properties and methods - also "contains"
whatever properties and methods exist in its parent(s).
In essence, inheritance allows you to share a piece of code between many
different types of objects. It avoids the need to copy and paste a commonly
used property/method into every single type of object that needs it - as
you might imagine, this would soon lead to a script that contains more
duplicated than original code (and would be a nightmare to write, debug and
maintain).
Here's an example:
--------------
script parentObj
property foo : 3
--
on getFoo()
display dialog "FOO=" & foo
end getFoo
end script
script childObj
parent property : parentObj
end script
tell childObj
getFoo()
--> displays dialog: "FOO=3"
get its foo
--> 3
end tell
--------------
Now, because AS supports prototype-style OO, things can get a bit
interesting when you have multiple children all inheriting from a single
instance of the parent object. In this situation, the values in the
parent's properties are _also_ shared between all instances of child
objects - tell one of those children to change a property value in a shared
parent, and all children will see that change.
More usually, you don't want the values of properties in a parent object to
be shared between children, so you create a new instance of the object each
time. To instantiate multiple instances of an object, you should either
copy an existing protoype object:
copy childObj to newInstanceOfChild
(this is probably the simplest to understand), or use a constructor
function to create and return new objects as you need them:
on newObject()
script
property foo : 3
--
on getFoo()
display dialog "FOO=" & foo
end getFoo
end script
end newObject
newObject()
--> <<script>>
But this is sort of drifting off-topic, so I'll stop there. Still, hope
this all makes some sort of sense.
has
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.