Function Reference: Difference between revisions
Jump to navigation
Jump to search
m
no edit summary
imported>Chesko mNo edit summary |
imported>Chesko mNo edit summary |
||
| Line 248: | Line 248: | ||
[01/01/2016 - 11:36:28AM] Calling foo on ScriptA | [01/01/2016 - 11:36:28AM] Calling foo on ScriptA | ||
It appears that '''foo()''' on ScriptB is called <b>twice!</b> The reason for this is because '''parent''' attempts to call the function <b>exactly one level higher</b> in the inheritance hierarchy, and does not "skip" a level in the hierarchy when the function isn't found. As we know, if a function isn't found on a child script, parents in the hierarchy are checked for that function instead. | It appears that '''foo()''' on ScriptB is called <b>twice!</b> The reason for this is because '''parent''' attempts to call the function "as" a script <b>exactly one level higher</b> in the inheritance hierarchy, and does not "skip" a level in the hierarchy when the function isn't found. As we know, if a function isn't found on a child script, parents in the hierarchy are checked for that function instead. If the function is found on another script higher in the hierarchy, it is called, but "as" the '''direct hierarchical parent''', not as the script on which the function is found! | ||
In this case, since ScriptC doesn't define '''foo()''', it looks at its parent in the hierarchy and calls ScriptB's version of '''foo()''', but "as" ScriptC. Since ScriptB's '''foo()''' calls '''parent.foo()''', it moves up the hierarchy one level higher and calls the same function again, only "as" ScriptB this time. This leads to the same function being called twice. | In this case, since ScriptC doesn't define '''foo()''', it looks at its parent in the hierarchy and calls ScriptB's version of '''foo()''', but "as" ScriptC. Since ScriptB's '''foo()''' calls '''parent.foo()''', it moves up the hierarchy one level higher and calls the same function again, only "as" ScriptB this time. This leads to the same function being called twice. | ||