Monday, February 12, 2007

Prototipo, Rubí

My English is native. My Spanish isn't, and is limited to certain usage situs. So no surprise, I express myself better in English.

As a programmer, Ruby is my English, JavaScript my Spanish. No surprise: I like to write JavaScript in a Rubyish way. It matches better how I want to think and makes things just flow better. Prototype.js makes this possible.

There are three particular Rubyisms that prototype.js gives me access to in JavaScript: [1] class-based OO, [2] modules as mixins, and in particular [3] the mixin Enumerable, which helps me to think functionally.

JavaScript is really prototype-based OO -- clone an existing object and extend it to create a new type. With protoype.js's Class.create(), I can pretend I'm just defining a class, whose #initialize method gets called upon instantiation.

Since I now have (something functionally equivalent to) classes, I can use Object.extend() to mix in modules.

Now, sadly, JavaScript doesn't encourage functional programming like Ruby does -- with its implicit returns and its blocks as method arguments. Nothing we can do about JavaScript's lack there -- we have to return explicitly, and we have to pass full-on methods, which if on some object, must be #bind-ed so the closure has the proper value for "this".

However, prototype.js still makes functional-style programming fun, giving us an Enumerable mixin rivaling that of Ruby's.

So prototype.js may not take us all the way there, but by making JavaScript a lot more Rubyish, it allows me to continue to program in the same style -- and think in the same way --, regardless of the syntax.

No comments: