Language Guide
CHAPTER 9
Script Objects
272 Inheritance and Delegation
The syntax for defining a parent script object is
( property | prop ) parent : variable
where
variable is a variable that contains the parent script object.
A script object must be initialized before it can be assigned as a parent of
another script object. This means that the definition of the parent script object
(or a command that calls a function that creates the parent script object) must
come before the definition of the child in the same script.
How Inheritance Works 9
To understand how inheritance works, think of a child script object as
containing a hidden copy of each of the handlers and properties inherited from
its parent. If the child does not have its own definition of a property or handler,
it uses the inherited (hidden) property or handler. If the child has its own
definition of a particular property or handler, then it ignores the inherited
property or handler.
Figure 9-1 shows the relationship between a parent script object called John
and a simple child script object called Simple. The figure includes two
versions of the child script object. The version on the left shows the actual
script object definition for the child script Simple. The version on the right
shows how the script object definition would look with the inherited properties
and handlers copied in. The inherited properties and handlers are shown
between dotted lines, to indicate that they aren’t actually a part of the script
object definition for Simple. As you can see, Simple inherits the
HowManyTimes property and the sayHello handler from its parent.
Figure 9-2 shows another parent-child relationship. As in the previous
example, the child script object inherits the HowManyTimes property and the
sayHello handler from its parent, John. But this time, the child script object,
called Rebel, has its own HowManyTimes property, so it doesn’t use the one
inherited from the parent. In the figure, the inherited property that is not used
is crossed out.