A Simple “Destructor” Pattern In Javascript

June 15, 2009 · Posted in programming · Comment 

I’ve been playing around with Javascript a lot more at work lately — in fact, the UI for the current project I’m working on is almost entirely done in Javascript (using the Dojo/Dijit/Dojox library).

In working on the project, I’ve been trying to follow some general “best practices” — not necessarily Javascript practices, but overall programming practices.  One of the main ones I’ve had some fun playing with is a basic destructor bastardization, and I figured what the hell: I’ll throw it out for people to take a look at.

Three notes I want to put out here first:

1) I’m using the Dojo library, but the pattern itself isn’t really library dependent.  The example code I’m giving should translate fairly well to Prototype, jQuery, or even just plain ‘ol vanilla Javascript.

2) Javascript doesn’t have an actual destructor implementation, like an “OnDelete” event or anything like that, so that’s why I say this is a “bastardization” of a destructor — you still need to manually call everything yourself in order for it to work.  But, setting that up to happen automatically when the page is unloaded is a part of the example code I’ll give out here.

3) This may or may not be good practice overall.  I’m not a JS guru by any stretch of the imagination.  I actually started doing this because of a misunderstanding — in FF, my project was leaking memory like a seive, and I thought cleaning stuff up would help.  It has, but most of the memory leaks were because of Firebug holding on to references.  However, I don’t think this code will hurt anything, other than spend a few cycles cleaning things up when the user navigates away from the current page.  If you find out otherwise, please don’t hesitate to let me know.

Anyways, on to the code…

The pattern is really simple.  To start with, we’ll create a very basic Javascript class (again, this is using the Dojo library; tweak to suit your own needs):

dojo.declare("Person", null, {
name: null,
address: null,

constructor: function() {
this.name = "Bob Jones";
this.address = "123 Test Dr.";
},

destroy: function() {
delete this.name;
delete this.address;
}
});

That last function is what this little article is about — “destroy”.  Yes, this is a contrived example, so in the real world you’d probably just let the JS garbage collector handle such a trivial object, but humor me.

Anyways, that’s half the fun right there.  The other half goes into the web page where you’re loading the object (again, using Dojo-specific code):

var thePerson;
function initPerson() {
thePerson = new Person();

dojo.addOnUnload(destroyPerson}

function destroyPerson{
thePerson.destroy();
delete thePerson;
}

dojo.addOnLoad(initPerson);

Of course, that goes in a script tag (I put it at the end of the page, but whatever).

Again, it's such a contrived example that it probably doesn't seem to be of much use -- even if a Person object did leak, so what?  Maybe it won't do a whole hell of a lot as far as overall memory consumption goes, but (IMO) it looks like a better attempt at good practices.

Please feel free to criticize and point out problems with the concept -- again, I don't claim to be any kind of expert on Javascript, so I may be missing the boat entirely here.  And I've been having some minor problems trying to get some good profiling set up -- even with Firebug -- so I couldn't really tell you whether it makes much of a difference in long-term memory consumption or not.

Anyways, I need to start getting ready for work.  Catch you on the flip side.

--Matt

  • Share/Bookmark

Just Trying to Get Back Into Posting…

June 7, 2009 · Posted in General · Comment 

Well, so much for doing a better job of keeping up with this thing.  What can I say?  It’s been busy around here lately — mostly trying to keep up with the new job and whatnot, but also “normal” life stuff as well.  Been getting new furniture, trying to keep up with housework, trying to relax as best as possible.

Still no writing, but I’ve been getting the urge a lot more lately.  And, always one to look for signs where ever they may be found, last night I got a little more of a push in that direction.  I was at Shooter’s (my dad’s bar) hanging out and watching the scotch doubles tournament, when one of the regulars came up to me and started asking me what she should do if she wanted to write a book.  Of course, I blabbed on with my suggestions for quite some time — don’t know if I helped her out or not — but it was one of those things that just kinda got the ball rolling again.  Especially since yesterday I’d been thinking a lot about maybe starting a new project.

Life’s kinda interesting that way, how “signs” seem to pop out of the woodwork like that.  But, then again, if she’d approached me even if I hadn’t been thinking about it, would I have considered it a sign?  Maybe, maybe not.  It’s one of those things that doesn’t bear thinking about too deeply, less you drive yourself bonkers with it.

For what it’s worth, even though I haven’t been posting much lately, my blog(s) have been in the back of my mind a lot lately.  I still want to start doing more here, just haven’t really figured out what it is I want to do.  What I’d originally thought of doing was having a set of regular postings every week — one or two on programming, one or two on writing, throw in some “regular life” crap or commentary on news and stuff, mix well, chill and serve.  Just can’t seem to bring myself to sit down and do it though — not even organize it or plan it.

Anyways…

I think I got up a bit too early this morning, so I’m going to rest my eyes a little bit before we get the day started.  Sunday’s have somehow turned into household-chore-day recently, so I probably should be a bit better rested.

Catch you on the flip side.

–Matt

  • Share/Bookmark