Jump to content
  • Advertisement
  • entries
  • comments
  • views

Features I can get used to

Sign in to follow this  


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.
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!