dean.edwards.name/weblog/2008/01/improvements/

Improvements to the Base Class in base2

A while back I published my Base class for JavaScript inheritance. There have been a few more solutions to inheritance since then and most of the major JavaScript libraries contain an implementation of some kind.

The implementation of Base has changed very little in base2 but there are some improvements which I will briefly outline.

Dynamic Object Detection

base2’s detect() method can be used to detect browser features or for straightforward browser sniffing. A special syntax can be used when extending objects that incorporates the detect() method. If a property name starts with “@” then the remainder of the name is passed to detect(). If the detect() method returns true then the associated interface is applied. Reading that back it sounds horribly complicated. Actually it isn’t and hopefully this simple example will illustrate the idea:

var Browser = Base.extend({
  // Default method:
  info: function() {
    print("I am a happy browser!");
  },

  // User agent detection:
  "@MSIE": {
    info: function() {
      print("I am a sad browser!");
    }
  },

  // Object detection:
  "@!(document.getElementById)": {
    info: function() {
      print("I am a very old browser!");
    }
  }
});

var browser = new Browser();
browser.info(); // => (platform specific)

If you like these changes and would like me to update the original Base class then I’m prepared to do a little bit of work…

Comments (9)

Leave a comment

Thanks for your continued ideas.

I like these changes and I could see them coming in handy when using base2. I am sure it wouldn’t take that much work?:)

So I am for yes, I would like to see these suggestions.

Bye.

base2’s detect in itself is really cool.

“If you attempt to extend an object with a method that is already in the inheritance chain then that method will not be added. This avoids circular references.”

This is also great, nice updates (yet?)

  • Comment by: Andy
  • Posted:

I think I would feel really bad if you were to go back through the work of updating the original base class, so I am sure it can be handled alright. Don’t ask why but for some reason I hit one of those “uhh” moments when thinking about dynamic obj. detect. But your definition really helped and just happened I stumbled across it. Thanks for this post I am looking over your original base class right now. Thank you again!

  • Comment by: lynne
  • Posted:

Just adding my support and saying great work. I love the base class because it’s so lightweight and easy to implement.

What Franklin said.

Just found this page – Base2 looks very, very useful, thank you!

  • Comment by: Timmy
  • Posted:

I would first and foremost want to thank you for a great piece of software and yes, I would like to see these improvements in Base.

  • Comment by: Fannar
  • Posted:

In the end I decided against including these changes in Base.js.

Base.js is pretty simple and it is probably best kept that way. If you really need these enhancements then use base2.;-)

  • Comment by: -dean
  • Posted:

Dean,

But Base2 does a lot of other stuff, over and beyond the inheritance stuff that Base does, and is, therefore, a much bigger library. Plus, I’m using jQuery, so I don’t need all the other stuff that Base2 does (I think!)

Is there a way of cutting down Base2 to give a version that does the inheritance stuff only?

I tried it myself and failed miserably!

  • Comment by: BrownieBoy
  • Posted:

Comments are closed.