Saturday, September 24, 2011

How I like to define JavaScript Objects

I've been really impressed by the expressiveness and power of JavaScript lately.  It's much more capable a language than I ever used to give it credit for.  The flexibility of JavaScript also can make it often harder to understand what is going on.  There are many ways to accomplish the same thing and they can look like completely different things.

If you have need to create a lot of custom "classes" in your application, I have a few things I've grown to appreciate.  In this post I'll talk about a style of class definition that I find readable, provides good encapsulation and can provide good performance when accessing functions on these objects.  I'm sure that if I'd worked with JavaScript longer I'd be able to enumerate all the problems with my approach, but so far I don't think there is a lot of down side. But I think part of the reason I like my approach is that it reminds me of Java with less ceremony.

Here's what I usually see around the web:
function MyClass (some, params, needed) {
    this.doSomeInit();
}
var p = MyClass.prototype; //Or = new ParentClass();
p.baz = "lorem";
p.fuzz = "ipsum";
p.foo = function() {
    return this.baz + this.fuzz;
};
p.bar = function() {
    return this.fuzz + this.baz;
};
I've also seen:
function MyClass (some, params, needed) {
    this.doSomeInit();
}

MyClass.prototype.baz = "lorem";
MyClass.prototype.fuzz = "ipsum";
MyClass.prototype.foo = function() {
    return this.baz + this.fuzz;
};

MyClass.prototype.bar = function() {
    return this.fuzz + this.baz;
};
So what's going on?  
The above code is basically just 2 different ways of defining a JavaScript "class".  I put class in quotes because JavaScript doesn't technically have classes.  It has objects.  My main gripe with these approaches is that it requires that these objects expose a lot of their internal structure by updating their prototype so that these public functions can manipulate the internal state of the object and do work. The only real difference in the 2 styles above is that one is slightly more efficient since it avoids the chaining of object property calls that's required.  There is a cost to calling MyClass.prototype each time you define a new function.  So if you're looking for speed you put that in a variable.

I prefer to do something like the following:
function MyClass (some, params, needed) {
    var baz = "lorem";
    var fuzz = "ipsum";

   function myPrivateFoo() {
       console.log("PRIVATE");
   }

    this.foo = function () {
        return baz + fuzz;
    };    
    this.bar = function () {
        myPrivateFoo();
        return fuzz + baz;
    };
}
This approach means that I don't have to expose the variables baz and fuzz.  I also get to use the parameters, "some", "params", "needed" without the need to create internal vars or properties on the prototype.  The functions foo() and bar() are both public privileged members of our object.   It also makes it easy for these public functions to use private functions.  My main reasons for liking this approach is that I think it's cleaner in that I like that my constructor encapsulates the class definition like you'd see in Java. I also like that my object can be more shy about what they expose to the world.  But is there a downside to this?  Actually there is.  

The catch is that every time the constructor is called all of the code in my constructor gets run.  All the function definitions are added to the prototype every time.  That's not so good.  Why?  Because if your custom object needs to be constructed a lot, then yes you'll take a hit in the performance department.  However that's usually a smell of something else not being quite right in the design and can be mitigated with a pattern that separates construction from execution and ensuring you are being judicious about your object creation. However if it truly is something that needs to be created thousands of times, then by all means follow one of the other approaches.  Or take a hybrid approach and define any functions that are fully public and don't require internal knowledge of your object state and define them explicitly with the MyClass.prototype.myFunction syntax and anything that has to access the internal state can be defined in the constructor.  There's not really a right answer here.  I'm just sharing my preference and providing a small argument for it.  

If you were to look at performance from another perspective, my approach may actually be preferred.  If you are using functions that an object exposes in a loop, or another pattern that requires lots of calls, my approach means that there are fewer property lookups on the object prototype since local variables can be used more often.  In practice I find that I'm not constructing custom objects very frequently, but I am calling functions on those objects in loops quite a bit.  So for me this approach is a good fit.  

Anyone else have a different take? I'd love to hear about it in the comments.

Friday, September 23, 2011

Starting Fresh

I'm no longer a part of the Redshirt Labs gang of 5 that was actually 4 for quite a while and felt like 2 for a long time, but whatever.  I'm done.   Of course we still hang out and eat the same lunch at Prince Al's, but no longer will I be pining over what to do next with Zengaku.  The rest of the gang will figure it out.  I'd offered to take it off there hands, but they didn't go for that.  I also considered licensing the source code and doing a fork, but I reconsidered and felt like a clean break would be best.

So begins the adventure once more.  This time with a little more solitude.  But if I make an application that only sells a few hundred dollars worth on the market, that's a bit easier to swallow than when you have to split that 5 ways.  Plus I'm not going to be making games.  Which I think would be fun, but is a tough business to get into and requires a lot of talent that I don't have.  Mainly a lot of art, music, sound and game design skills.  I could probably implement the code for a game if you gave me the spec/design docs, but coming up with all the rest is way to much for me and I can't go and hire all that talent either.  This is just a hobby after all.

So I've made the initial steps of setting up my new business.  I've got an idea for a first application.  I'll share that soon enough. I got a website, a hosting service, a remote repository and an official business license.  I'm working on my registration with the Apple Developer Program. It's all getting started.  Now to start making the application.  My hope is to have it delivered to the App Store before the Christmas holidays.  Not that it would help sales or anything, just that I need a goal and that seemed reasonable.

I'm not going to get into the details of my business idea, or the application I plan to build just yet.  If you ask me in person I'll tell you about it. But mostly I'm just not ready to share much else at this point.  I have a lot of work to do.  I'd like to have a decent landing page on my website, get some blog content lined up,  get the application in progress and moving forward and so on and so forth.  Then I'll have loads more to talk about. But for now this is about it.

I may have to write a bit about my new affection for JavaScript and functional programming.  I don't know if I'd say I know a lot about either just yet, but I've definitely spent some time getting familiar with JavaScript over the last couple months and I'm actually quite impressed.  But also looking forward to doing a bit of C/C++ work once more.