Posts Tagged ‘JavaScript’

Simple Javascript Custom Event Object

Monday, March 30th, 2009

Custom events are a fact of life. The Observer Pattern is indespensable. YUI supplies a custom event object - but to get it, you’ve got to include about 8 kilobytes of library in your project. Granted, YUI supplies much, much more than just an event object - but sometimes you don’t need all that. You just need a way for objects to talk to each other. Introducing a bare-bones event object that’s good enough for most situations! It’s not as flexible as YUI’s, but it gets the job done.

(more…)

Javascript Inheritance benchmarks, redux

Thursday, March 26th, 2009

The other day I posted examples of Douglas Crockford’s parasitic Javascript inheritance method. I’d like to qualify some of my claims with some benchmarks using JSLitmus. The skinny: Parasitic inheritance is, indeed, faster. It’s slower to initialize, but unless you’re creating thousands and thousands of instances, that shouldn’t be a problem. It’s faster on calling superclass methods, and calling non-overridden methods. And you don’t need a bulky library to do it. Read on for the results. (more…)

Javascript Inheritance to protect your variables

Wednesday, March 18th, 2009

Javascript inheritance is a multi-horned beast. You can use classical inheritance to make things work a little bit more like Java, or you can subscribe wholeheartedly to doing things Javascript’s way.

Doing things Javascript’s way can be a boon, once you wrap your head around it. You don’t need any helper functions to help you out with this. You don’t need a library to implement useful inheritance, like jQuery or Prototype. You gain the benefits of being able to define private variables for your objects. You do lose a little bit in terms of memory usage. But these days, memory is cheap - and Javascript engines are only getting faster. So why not make it easy on yourself, as a developer - and your users, as downloaders?

You’re also limited in that it’s difficult for an overridden method to access the object’s parent implementation. However, I’m firmly of the belief that if you need to do that, you’re doing it wrong. So, without further ado, let’s delve into how to use Javascript to inherit in style - the Crockford Way.
(more…)

Private object members via closures

Sunday, March 1st, 2009

In almost any object oriented language, you can define access levels for variables in objects. You can in JavaScript, too, but it’s not quite as clear cut as adding a “public” or “private” key word to your object definition. Once you understand how closures work, though, it’s pretty obvious how to implement private member properties and methods. (more…)

The Strategy Pattern (Part 1)

Saturday, February 21st, 2009

The Strategy Pattern is more useful for programming languages that do not support first-class functions. Fortunately, JavaScript treats everything as an object, and allows for run-time object modification. That enables the use of the strategy pattern quite nicely.

Here’s a simple example for changing the behavior of an object without hard-coding the behavior options into the object itself. It allows for flexible changing of object behavior down the road, without having to refactor an entire code block.

(more…)

Faking Interfaces in Javascript

Saturday, February 21st, 2009
So you’re used to standard design patterns. You come from a C# or Java background, and you miss being able to abstract things out with interfaces. I won’t go over when you should use interfaces, but I will show you how to “fake” them in Javascript.

(more…)