Hi everyone,
I have been trying out the new javascript functionality and although
I expect I am not the only one, there is precious little
discussion/comment/opinion around so I thought I might start some.
I like the idea of using javascript over lingo for several reasons,
but there seem to be a few problems with the object oriented parts.
Admittedly, as a C++ programmer, I thought it unfortunate that java
didnt have "friends", so my standards are quite high. Moving to
lingo, I found Parent/Child scripts quite frustrating, but,
similarly to everyone else, you find ways to do what you want to do
which work, and you stick to them. If you move to using javascript
then it seems you need to go through this process again.
Firstly, I cant find way anyway for behaviours to inherit.
Inheriting in behaviours (instantiated through the beginSprite
rather than the new method) was a bit flaky before; initialisation
was the main problem (e.g. you needed to pass the spritenum down to
the ancestors constructor). With javascript you dont directly
instantiate the base class - you set the prototype and the
constructor gets called and the properties get set immediately
before the subclass gets instantiated (but only when you new
something - not when you beginSprite it)! So, using inheritance in
behaviours never worked as would like and was best avoided. With
javascript this decision (unless you know better) is made for you.
Overriding methods and properties works better (in some ways) in
javascript than in lingo - in lingo you could end up with an
inherited method updating a property in the ancestor which was
hidden by a property of the same name in the child depending on
where "me" came from. In javascript, "this" seems to reliably
correspond to the child (as long as you dont start playing with the
__proto__ property).
I tried to use the __proto__ property like I might use the ancestor
property (or use callAncestor), but I cannot see a way to call a
method on a base class once it has been overridden. I often used
this to chain initialisers, for example.
Initialisation is an issue therefore. As the contructor of the base
class gets called before the constructor of the subclass it doesnt
make sense to initialise properties except in leaf classes. Where
initialisation is complex (e.g. objects registering with container
objects) it seems best to keep this completely separate from the
construction. Currently, I am thinking that using static/class
methods (which you can now do), with the instanceHandle passed as
the first parameter is the most effective (it allows calls to be
chained without unnecessary initialisation or instantiation).
Presumeably many of you have come across these issues before from
using javascript in browsers, but I expect that you are prepared to
write a lot more code in Director than you would write for a
browser, so OO is more likely to play a major part.
Oh, one other thing, you cant inspect a javascript object like you
can a lingo object in the debugger or object inspector (now how hard
can that be!).
Ben