object getProp() / setProp()

I have nev­er seen in any doc­u­ments that Lin­go allows prop­er­ties to be added to an already exist­ing instance.
For example:


-- parent script "testScript"
on new( me )
  return me
end new

-- type in the message window
obj = script( "testScript" ).new()
obj[ #speed ] = 200
put obj.speed  -- 200

When I decid­ed to study more care­ful­ly this func­tion­al­i­ty, I found that get­Prop() and set­Prop() bring dif­fer­ent results com­pared to getAprop() and setAprop(). This becomes evi­dent when the object, with prop­er­ties created/modified by us, is a script instance inher­it­ing anoth­er script. For exam­ple: There are three par­ent scripts (“A”, “B”, “C”). “C” inher­its “B” and “B” inher­its “A”:

 

-- parent script "C"
property ancestor

on new( me )
  ancestor = script( "B" ).rawNew()
  callAncestor( #new, me )
  return me
end new

-- parent script "B"
property ancestor

on new( me )
  ancestor = script( "A" ).rawNew()
  callAncestor( #new, me )
  return me
end new

-- parent script "A"
on new( me )
  return me
end new

If we write in the mes­sage window

obj = script( "C" ).new()
obj.setProp( #speed, 100 )
obj.setAprop( #speed, 200 )
put obj.getProp( #speed ) -- 100
put obj.getAProp( #speed ) -- 200

If we look at “obj” in the object inspec­tor, we will notice that:
set­Prop() cre­ates a prop­er­ty in the base script
setAprop() cre­ates a prop­er­ty in the last suc­ces­sor ( child )
get­Prop() gets the val­ue of the base script
getАProp() gets the val­ue of the last suc­ces­sor ( child )

 
Comments

No comments yet.