[mtasc] Re: [OT] Dynamic variables?
Timo Stamm
t.stamm at macnews.de
Tue Sep 6 18:50:48 CEST 2005
Firdosh Tangri wrote:
> dynamic classes are usefull when you want to add properties at run time for
> example
>
> var person:Object=new Object();
> person.name="John";
> person.age=23;
You could do the very same with a class:
class Person {
public name : String;
public age : Number;
}
...
var p : Person = new Person();
p.name = "John";
p.age=23;
And have the compiler check the type at compile time...
The disadvantage: You need you an extra class file (I guess AS doesn't
know inner classes?)
I think it is mostly a matter of personal preference whether you like
classes and compile-time checking or more freedom (and more potential
sources for runtime errors).
I like being able to create an object with as few keystrokes (and no
extra class) like this:
var p = { name : "John", age : 23 }
So this is basically a question of programming style. But there are
other reasons, see below:
> On 9/6/05, tcornel <tcornel at gmail.com> wrote:
>
>>Actually, i was wondering what's the deal with the dynamic classes,
>>and why not one who wants to add a method to a class would not simply
>>extend that class (inheritance).
Take a look at this extension of the built-in Array:
Array.prototype.unique = function () {
o = [];
for (i in this) {
d = 0;
for (j in o) {
if (o[j] == this[i]) {
d++;
}
}
if (d < 1) o.push(this[i]);
}
return o;
}
ASSetPropFlags(Array.prototype , null, 1);
usage:
[1, 2, 2].unique()
This would not be possible using a subclass.
It is similar with attachMovie, loadMovie, createEmptyMovieClip. You can
use registerClass with attachMovie, but in my experience it's best to
use a Delegator (the design pattern as defined by the GOF, not
mx.utils.Delegate) to wrap the MovieClip class, and derive your subclass
from that (that's entirely my opinion, though).
Timo
>>
>>regards
>>Cornel
>>
>>On 9/6/05, Till Schneidereit <tschneidereit at gmail.com> wrote:
>>
>>>One way to get around this problem is to use (static) subclasses of the
>>
>>built in dynamic classes whereever possible. That way, you get all members
>>of the superclass without having to deal with its dynamic nature ...
>>
>>>>This is very frustrating, i'm used to c++ and trying to learn
>>>>actionscript. So, i was wondering,are there some methods to avoid this
>>>>kind of errors? I really hate dynamic classes...
>>>
>>>cheers,
>>>till
>>>--
>>>MTASC : no more coffee break while compiling
>>>
>>
>>--
>>MTASC : no more coffee break while compiling
>>
>
>
>
> ------------------------------------------------------------------------
>
> --
> MTASC : no more coffee break while compiling
More information about the mtasc
mailing list