Javascript is a good language. I like Ruby better, but it is certainly better than Java (which is not a good language) and it has has certain unique charms. A dynamic, prototyped OO language with a runtime penetration of nearly 100% on desktop systems. I’m ashamed I haven’t done more with it.
This script from Florian Gross that adds several of the Ruby standard methods to Javascript is an excellent example how flexible and clean Javascript is. Below is a code snippit for adding Ruby style mixin support to Javascript. While it is impressive that Javascript is flexible enough to allow this sort of thing, the truly amazing bit is that it is seven lines of straightforward code that even a casual Javascripter such as myself can understand.
Object.prototype.extend = function(other) {
if (!this.mixins) this.mixins = []
this.mixins.push(other)
for (var property in other)
if (!this.hasOwnProperty(property))
this[property] = other[property]
}
Now for the practical aspects of programming Javascript Ruby style.
Here’s some brief documentation written by Florian. Also while using ruby.js in a script that interacts with the DOM, I ran into a bit of trouble. In Firefox at least the Array that is returned by getElementsByTagName does not have the added methods. I would appreciate any explanation as to why this is so, but there is a work around in the mean time.
Instead of:
foo.getElementsByTagName(‘tr’).each(function(row) {
…do something with the row…
});
You have to:
Array.fromObject(foo.getElementsByTagName(‘tr’)).each(function(row) {
…do something with the row…
});
|
|