Posts Tagged ‘Programming’

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…)

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…)

Tired of futzing with Expression Engine.

Saturday, February 21st, 2009

So, I’m switching over to wordpress. Admin area is much easier to use. Don’t know about themes yet. I’ll get on top of making one eventually.

I’m only bringing one post over from the old blog - the one on JavaScript Interfaces. The first set of posts here will be implementations of common design patterns in JavaScript.

Here’s to hoping it will legitimize the language in the minds of some, and help others to write JavaScript that actually makes sense.

Reblog this post [with Zemanta]