🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Features I can get used to

Published June 27, 2003
Advertisement
Ever get used to a feature of a language that's just do durned useful that it spoils you and you suddenly find yourself using it all the time and can't imagine not using it?

Well, JavaScript/JScript/ActionScript's object model is starting to spoil me in that way. In no way is it as rigorous and well-protected as C++ or even Java objects, but they're just so durned easy! My favorite example is that objects can be completely anonymous and definition-less. Coming from a C++ background where the idea of an object without a class just isn't in the vernacular, this was a concept around which it was difficult for your humble author to wrap his mind. For example. . .

Foo = new Object(); // instantiate an object
Foo.X = 37;
Foo.Name = "I'm a Foo!";

There, I just created an unclassed object and added two pieces of member data to it. The language has a constant called undefined that can be used to see if a value has been set in the object. . .

trace(Foo.X); // prints 37 to the console
trace(Foo.Y); // prints undefined to the console

This also means that you can perform the unheard-of act of removing members from an object. . .

Foo.Name = undefined; // removes the Name string from the object.

Again, it's not nearly as all-encompassing as C++ objects, and being able to add members willy-nilly is probably a Lispesque overly-dynamic performance headache, but for small stuff like my games, it's downright convenient.

It's also quite nice for objects that may or may not contain certain members. I For example, a cell in Duck Tiles can contain one or more of the following:

A tile
A drain
A ducky
A wall above
A wall to the left
A sponge

In C++, I would need to have storage defined for every one of these items (be it a pointer or index or the object itself), leaving most of them NULL because most cells would contain at most one or two of these items. If I had a larger project and object-fat became a problem, I could make all of the bits derived from some kind of BoardObject class and have the Cell object contain some kind of dynamic storage for BoardObjects, and have the objects able to identify themselves via a polymorphic function or RTTI.

As you see, it's a problem that could be solved in a way that is as non-elegant or as elegant as you want, but in any case it's gigantic overkill for something as simple as a Duck Tiles board. With JavaScript/JScript/ActionScript objects, I can just add the members necessary to my array of objects and check to see if they're defined later, which seems like a much more simple solution.

Again, the elegance is probably an illusion, as the dynamic allocation required to support such a model will wipe out any potential memory or performance advantages of the approach.

It does, however, make ad-hoc solutions quite simple. For example, I was wondering how I could identify a particular movie clip as a duck if it's clicked. Suddenly the blindingly-obvious solution dawned on me. . .

TheMovieClip.duck = true;

Then I just check the "duck" member in the click-handler and I'm done.

I'm spoiled.
Previous Entry comments on comments
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement