A Simple “Destructor” Pattern In Javascript
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
Just Trying to Get Back Into Posting…
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
New Job
Quickie post to bring you up to speed.
Last Wednesday I started a new job, doing development work at a local insurance company. So far, it’s great — a bit of a pay cut, but it’s a great work environment and it’s pretty much a new department, so there isn’t really a lot of infrastructure set up yet. I know that, in a lot of ways, that can be quite annoying, but it’s also good in my book: it’s pretty much a blank slate, and we can get things rolling in a halfway decent manner.
The only thing that sucks about it is that the lead developer — my supervisor for these few days — is leaving on Thursday. So far, we’ve hit it off pretty good too, and I’ve only spent one day with his replacement (she’s just hanging around for a couple of days while he’s still working so she can get up to speed; she’ll start full time in May I believe), so I’m not quite sure what to make of her yet.
Once we get up to speed, there’s going to be a lot of pressure to start putting stuff out, but for now it’s a wonderful change of pace from my previous job.
As such, there’s no news on the writing front, and probably won’t be for a while as I adapt to the new environment. Hopefully, once I get better situated there, I’ll be able to focus a little more on my creative ventures. We’ll see though.
Need to get ready to go — only “down side” is I need to wear a tie and a dress shirt to work, so getting ready isn’t quite as quick and easy as it used to be. No biggie…
Catch you on the flip side.
–Matt
New Project in the Works
Working on some ideas for a new sci-fi story. The plot I’m working with is probably already a little over-done, but I’m having fun with it and that’s the main thing I’m looking for right now. I’m thinking about running it as a serial here on the blog — that’s the way I’m sketching it out right now, at least, as a series of “episodes” instead of chapters. Puts it in a different light, that’s for sure.
As I said, the plot’s probably a little over-done — colonizing a planet, basically — but I’m hoping to focus on the human elements more than the science in this story (i.e. emphasis on the “fi” in sci-fi), and hopefully that’ll bring a slightly different spin on it. Don’t get me wrong, I’m sure that’s been done more than a little bit as well. But think about it — how many times do you hear the same, basic plot line(s) retold with different settings and characters? Besides, I’ve never really strayed into sci-fi yet, and it may be fun.
Okay, enough trying to rationalize it to myself. I need to start getting ready for work. Catch you on the flip side.
–Matt
Baseball and A Tragic Death
Alright, I think I’m about ready to wake up from my little bout of hibernation here and do some real blogging. Or, at least, my futile attempts at it, and I gotta couple of things I actually wanted to talk about.
First off, no news on the writing front — sorry about that. I’ve done a little bit of sketching, but I’ve had some … interesting stuff going on lately (that I’ll gladly discuss when the time is right), and it’s distracted me from a lot of my more interesting pursuits.
Okay, with that out of the way, I’m going to spend this summer — at least, I’m going to try to spend this summer — in a bit of a different mindset than I have in most years since I was about 11. I’m actually going to try and keep up with baseball. Blame the radio: I’ve been listening a lot to the ESPN radio station when ever I’m in the car, and I gotta say I love it. Well, the talk shows they have, at least.
Don’t get me wrong, I’m not a sports nut by any stretch of the imagination. I used to collect baseball cards, back in the early 90’s, and I don’t really have a clue what’s going on in the sport. I mean, sure, A-Rod and Jeter are always on the news for some reason or other, and there was the whole steroids fiasco, but I mean keeping up with the actual game.
So, this year I’ve decided I’m going to spend a bit more time checking the scores and maybe even watch a couple of games if I happen to catch them on the tube. I even have a handful of teams that I’m rooting for. In the national league (which I was never really that much into), I’m mainly going for the Cubs. The reason is simple: I’d just love to see them get a championship and break that damned curse. Not really expecting them to, although from what I hear they have one of the best teams in the NL.
Other than that, most of the teams I’m going for are in the American League. First off are the Tampa Bay Rays. Not because I actually like them, and not because they went to the series last year, but because I’d like to see them keep the Yankees and the Red Sox out of that dance.
I’m also going for Seattle, because they’re one of the few teams with a player I actually remember from when I was really into baseball: Ken Griffey Jr. Yeah, he’s getting to be an old man now — as far as sports are concerned — but still, it’s Junior man. I’m glad to see him back in Seattle. And, I’ll admit, there’s still a bit of a “root for the home team” mentality with them as well. Not that I was much of a Mariners fan when I was in Washington, but it’d be kinda nice to see them do something before Griffey retires (damn, I never thought I’d ever say anything about Griffey retiring…).
But, the main team I’m going for throughout the whole league is Texas. To give you an idea how long it’s been since I’ve kept up with them, the last couple of things I remember from the Rangers were 1) A homerun that went off Jose Canseco’s head (tee-hee), and B) Robin Ventura charging the mound and Nolan Ryan beating the crap out of him (check out this link for a video clip of it, or this one for a full “top 10″ of baseball fights, with the Ryan vs. Ventura altercation as a well-deserved #1. Both links open in a new window.).
I do want to make it clear that I have no misconceptions about the Rangers going all the way. It’d be great, don’t get me wrong, but I really don’t expect them to do too much. Maybe make the playoffs, but even that I’m kinda doubtful on. I’m mainly going for them because, well, I feel I need to go for someone, and the Rangers are a little nostalgic for me — mainly because of Nolan Ryan, who was pretty much the main player I loved when I kept up with the game better.
And yes, I know they just got massively and painfully ass-raped, without lube or a reach-around by the Tigers yesterday (4/10). You don’t see a 15-2 loss by any team everyday, so getting reamed like that is actually quite amusing. And besides, they still swept the Indians, so that ain’t too bad to open the season with (then again, the Indians could be the worst team in the league for all I know; I don’t really care though).
On a more somber note though, I do want to talk a bit about what’s going on with the Nick Adenhart scenario. First off, it’s a damned shame what happened — if you don’t know what I’m talking about, you can check it out at CNN. Poor kid was just getting things started, and to have his life cut short in any way, let alone the manner in which it happened, is just sad.
I’ll be honest, when I first heard that the guy who caused the wreck had been drunk, it didn’t surprise me. Nor that he’d fled the scene — pathetic, but he’s 22 years old, and probably scared shitless. I’d like to think I would have stayed at the scene, but (thankfully) I haven’t been in such a situation like that so I really don’t know what I’d do.
What did surprise me — at first, at least — was that he’d been charged with 3 counts of murder. Murder? I knew he’d get charged, but I figured it’d be under vehicular manslaughter, or something a little more minor than the big M. Not to trivialize what he’d done, but that’s what I thought was standard in DUI deaths. I could be wrong, but I believe that’s what the kid who killed Sam Kinison was charged with — he was drunk, and charged with vehicular manslaughter, not murder.
So, that was my first thought — they’re gonna run this guy over the coals for a stupid accident, probably just because it was a rather high-profile death. I’ll admit it, I’ve driven with a bit more alcohol in my system than I should have, so while I don’t condone what the guy did in the slightest, I can certainly understand his perspective a little bit (or so I thought): been out, having a bit of a good time, want to get home and pass out. In my mind, it was a tragic accident, but definitely not murder.
Then, later last night, I started reading more of the facts of the story and a bit more of the guy’s back ground. First off, his BAC was three times the legal limit in California. Jesus Christ! Three times? That’s not just a bit of a good time, that’s going-to-bed-with-Cindy-Crawford-and-waking-up-with-Edward-James-Olmos drunk. That much alcohol in his system, I’m surprised he could walk to the car, let alone drive it.
Next, the fucker was driving 65 MPH in a 35 MPH zone. I’m sorry, but if you’ve been drinking at all, that’s the last thing you want to do. Not just because you might, I don’t know, get into a wreck and kill someone, but because you’re just asking to get pulled over. The fact that it was a minivan just makes that even more absurd. Sorry, but every time I’ve driven after even a single beer I make damned sure I drive as safe as possible just to avoid a DUI.
And, to top it all off, this ass hat was driving with a suspended license. Why was it suspended? Because of a previous DUI charge. How dumb can you fucking be? Get absolutely sloshed, speed through a residential area (at least, I’m assuming it was residential because of the speed limit), when you’ve already been under the gun for drunk driving?
After hearing all those facts, I say fry the mother fucker, and I don’t care if it was a major league pitcher he killed or a damned child molester. I think the saddest thing of all is — as so frequently happens — the dumb fuck who’d been drinking so much lived through the accident, but he killed three people and put another one in the hospital in critical condition.
Okay, rant’s over
I’ve got stuff I need to pretend I’m going to do, so I’ll just catch you on the flip side.
–Matt
Another “Yes, I’m Still Alive” Post
Been mostly trying to take it easy the past couple of weeks. Been throwing around a few ideas for some more novels, but nothing’s really stuck yet. Not worrying about it; they’ll come as they come.
There is, however, the possibility that I may have a good change in my life coming up, one that could (potentially) make writing a little easier. If it happens, I’ll definitely write about it here; I don’t want to jinx anything by talking too much about it yet though.
Probably should be working on some of those plot ideas — or trying to scrape out new ones — right now, but been up since about 4:00 this morning, and I’m already exhausted. Hopefully I’ll get a little bit of a nap later on today, but I’m not exactly going to bet the farm on it.
The main thing I have left to say is that I haven’t forgotten about Vanishing Point; it’s one of the novels I’m sketching out. I just haven’t gotten the sketch up to where I’d like it to be yet. Using a different method of writing now — instead of doing everything by the seat of my pants, I’m trying to get a little better organized, outline stuff (at least to some degree) and planning things out. Seems to be a little better so far, but of course I haven’t gotten around to actually writing anything yet, so I can’t really say for sure.
Okay, I think I’ve talked myself into doing a bit of sketching now. Catch you on the flip side.
–Matt
Still Kickin’
Yep, I’m still alive, and I’m still writing. Actually doing some sketching for Vanishing Point — just as an FYI though, that’s strictly my working title, and I’m not sure if it’ll stick or not.
I’m also going about this one differently than Like Glass or Shattered — I’m actually trying to outline this one, in hopes that I’ll be able to make a better story of it. I’ve found it to be difficult, trying to tell the story before I tell the story, but it does seem to be working out fairly well. We’ll see what happens.
Just wanted to drop a line and let you know I’m still amongst the living.
–Matt
Trying to Get Back in the Saddle
Back on the writing train. Been working out some plot details today for a new book — something entirely different from Like Glass. More of a supernatural thriller. We’ll see what happens. Coming up with the ideas so far has been pretty interesting, especially since I’ve essentially given up on writing for a while. Might be neat to get something decent to come out of it.
Still need to get something figured out for work — either fixing where I’m at or trying to find some place new. Don’t know what to do, but it’ll figure it’s self out soon, I’m sure.
–Matt
This Should Not Come as a Big Surprise….
…but I think I might be stretching out my writing muscles again. It’s been a while, and I’m not making any promises (because I know I’ll just break them as soon as I make them anyways), but I think I might be opening the door to Rob’s world once again. Might even re-neg on one of my earlier posts and actually go through with editing and publishing Shattered. Should be interesting.
That’s all I’m going to say for now though; keep your eyes peeled.
–Matt
So Much for the 27 Club
Well, if nothing else, I’ve officially passed any opportunities I may have had for joining the 27 club — I’m officially 28 today (and besides, I think you have to be famous — especially as a musician — for you to be part of said club…). So, I’ve spent the day reading quotes from bash.org and doing absolutely nothing. Of course, that’s pretty much what I’ve been doing every day since I got back on leave, so it isn’t anything special.
Got a message on FB from a friend of mine who finished reading Like Glass, and (of course) they said it was great. Kinda irks me — and don’t take this the wrong way, if you happen to catch this — but it makes me wonder, if the damned book is so “great”, as everyone has said it is, why the fuck couldn’t I get it published? I dunno… Not gonna worry about it anymore; I think I’m done with that side of my life for now.
Was giving a half-assed thought to starting up my own company again, “Chocolate for Dogs Productions” (or something stupid like that). Sell my books, music, and software under one name, all relatively cheap. Not worth it though, especially when I’d have to worry about filing taxes and everything. It’d be fun to screw around with the stuff for a month or two, and then I’m sure I’d get tired of it.
Also, and this is one of the reasons I haven’t done much posting lately, I’ve been kinda keeping my eye out for another job. That’s a joke though. There’s no jobs out here, nothing that I can do with the skills I have and make as much money as I’m making now. I dunno….
I’ve been trying really hard to try and keep my chin up for a long time (I know it probably doesn’t sound like it most of the time), but the fact is nothing gets better, and I’m fucking tired of it. Meds don’t help. Therapy didn’t help. I don’t want to deal with either of those any more — I stopped doing the therapy stuff, and I’m seriously thinking of talking to my doc at the next appointment and dropping the meds too. It’s a waste of fucking time and money — and there isn’t a lot of the latter to waste any more.
Okay, I’m going to bash.org again, get a few more laughs before I have to go pick the wife up.
–Matt
