Emulating catchall, getter and setter in JS

Getters and Setters are cool stuff, so is the notion of catchall. And, if it's a nice feature, it has to be implemented or emulated. I don't have nothing much to say about this except that I did it and that it works. The syntax isn't really nice, but it's usable. I used the same examples used here.

function CGSobject (catchall) {
  var o = function (k,v) {
    if (v) {
      var curv = o.content[k];
      (curv && curv.set && curv.set.call(o.content,v)) || (o.content[k] = v);
    } else {
      var v = o.content[k] || o.catchall(k);
      return (v.get && v.get.call(o.content)) || v;
    }
  }
  o.content = {};
  o.catchall = catchall || function () { return null };
  return o;
}

var o = CGSobject(function (x) { return x+1; });

// basic set
o("a", 7);

// basic get
print(o("a"));
// -> 7

// getter
o("b", {get: function () { return this.a+1;}});
print(o("b"));
// -> 8

// setter
o("c", {set: function (x) { this.a = x / 2 }});
o("c", 50);
print(o("a"));
// -> 25

// catchall
print(o(2));
// -> 3
print(o("foo "));
// -> "foo 1"

What Server Side JavaScript needs

A few of use are convinced that SSJS is the way of the future, in terms of elegance and simplicity (using just one language for both client and server), etc. However, if we wish that one day JS beat PHP1, there are several conditions.

First of all, I think the main drawback is inertia. PHP is currently the way to go in web development (even though a few still use ASP, CGI, etc.). In order to counter the inertia, any new server side language has to be as good as PHP (not so difficult to do), as easy to learn and have exciting new features.

Python and Perl have been around for quite some time, without becoming dominant, so I guess that they won't in the future.

Some said that RoR was that alternative, and let's face it : it's trendy among Mac geeks using textmate… Not that I have anything against this, but those developers live in another world: RoR has basically several flaws which can't be overcome. First, it doesn't scale2. Second, it doesn't have a C-like syntax3, and this has a psychological impact on devs coming from the PHP world. Third, it's yet another language to learn. Fourth, it didn't play well under Apache until recently. The reason why some guys switched to RoR could be summed up to three points: the MVC model, the Active Record pattern and scaffolding.

All those ideas are nice, but it's not enough for RoR to be the Next Big Language. SSJS is exploding right now, with quite a number of solutions being developed. However, most of those are bloated and are either ports of PHP or RoR to JS. The only one which takes it to another level is Jaxer, which is a nice concept, but is intrinsically unscalable: how can one imagine having hundred thousands instances of DOM trees being manipulated at the same time on the server and being delivered quickly ?

I believe that the JS strength resides not only in the fact that it's usable on the client, but also because it's prototype based. For example, Active Record is something nice for class based object orientation, but I believe that there are much simpler approaches using prototypes.

Nevertheless, unless someone comes up with some revolutionnary features for SSJS, I'm afraid that it'll stay a niche language (on the server).

  1. This is not a joke []
  2. For example, Twitter is abandonning RoR, for that reason []
  3. no curly braces, no semi-colons []

This is the future

According to this article, a team at HP Labs have developed a new circuit element (the memristor). I'm not really into physics, but basically it allows persistant storage of data: if you flow the charge in one direction, the resistance increases, if you flow it the other way round, it decreases.

I won't detail more than that, as the article and its comments give a lot more information that I'd be able to. From times to times, I read something and I just think we're living in the future, this will be everywhere, eventually.

[whatever] in JavaScript

Those last days, there has been a lot of fuss all around about Orto1 and HotRuby. I've also heard of a project of OCaml bytecode interpreter in JS, though I have no link for it at this time. Having some Java/Ruby/OCaml bytecode interpreted in JavaScript is quite nice, but I just can't see what's new/interesting about that.

There are quite a few other languages for which a JS interpreter is available: Basic, Scheme, Lisp, Prolog. Even Javascript can be interpreted in JS using Narcissus2. If this is not enough for you, I can add Brainfuck or even other esoteric languages.

I guess you'll say that all of those bring nothing interesting to the browser and that using compiled bytecode instead of parsing/interpreting source code is much faster. The latter can be easily forgotten, what about an x86 interpreter3 ?

Nevertheless, what is the easiest thing to write ?

  • plain JavaScript, with a powerful library such as JQuery
  • some bloated Java using cluttered GUI tools which were not built to output web components

A few years ago, writing some JavaScript meant dealing with cross browser issues. Today, JS is much more friendly, thanks to a lot of libraries. Moreover, I'm pretty sure the next move will be SSJS4, is there a reason (other than because I can) for porting Java/Ruby to JS ?

  1. John Resig talks about it here []
  2. It's disturbing, I know []
  3. Some guys really have a lot of free time.. []
  4. I'll write an article about that someday []

Memories

About 16 years ago, I got my first computer which was a Tandon running Windows 3.1. My dad, who used to use it for work had brought back home a Digital station and had no use for the old machine anymore. Anyway, that's how I discovered the joy of MS-DOS when I was not even 7 years old. Actually, I was not that geek, my main activity on the computer were two games: Sim City and Populous1.

A year later, in 1993, my mum brought back a Compaq running Windows 3.11 with a CD-ROM drive. I never used that machine, because at that time I spent my time on a little game on my dad's computer2. That same year, my dad came back from the US with a brand new game: Doom. The day I tried it, I had a revelation: I could not stand FPS3.

In 1996, my parents bought a new computer, a Pentium Pro 200 Gateway4, with a 17" monitor and Windows 95. Six months later we had an Internet access. I remember that the first thing I searched using AltaVista was Lego, followed by Star Wars… Some of my parent's friends told us about a software called ICQ, and that was the beginning of a long story. When I was not chatting, I used to play to Civilization II, Myst and Warcraft.

In 1997, I remember playing a demo of a gore game which I fell in love with: GTA. I acquired the game and spent long hours tweaking it, which was real fun. I also spent numerous hours on Age of Empires, but I was too lazy to not use cheat codes. This was my last RTS experience.

A few years later, around 1999, we got a cable connection. Internet became something different: this permanent extension of existence. Terrifying. In April I downloaded my first mp3 from a chinese web page hosted on Geocities and discovered Napster a few months later. Google arrived in my life and I never used another search engine since. In the mean time, I started having some fun with ASP and VBScript, and this was my first programming experience5.

By the end of 2000, I spent my time playing GTA II. I got a free domain name on NameZero and created Copkilla.net and hosted something which was meant to be a fansite. And I was still using the Gateway…

The next year, I bought my very own computer and I discovered T4C6, my first MMORPG experience, and became an addict. I spent my life online: when I was not playing, I was on some T4C forum. I spent almost 4 years on that game, and in 2003 became Game Master and eventually Admin of a french official server. No need to say that my whole life turned around that game.

In 2003, a friend sold me a motherboard with a PIV and a GeForce V for ~20€, which was a really cheap upgrade. I got my baccalauréat and went to classe préparatoire and I had to choose between computer sciences and engineering as an option. I obviously took computer science, and this is how I met Caml and functional programming.

During the summer of 2004 and 2005 I worked at MonsieurPrix.com/Kelkoo.com. The first time I wrote a robot to scrape Amazon's catalog and insert it in the products database, the second time a robot to submit loads of keywords into Google Adwords, both projects were written using ASP.

By the end of 2005 I launched my personal blog, which was a mere conglomerate of thoughts, poems and such. I started tweaking my WordPress install and got familiar with xhtml and CSS.

In 2006, I entered the Ecole Normale Supérieure de Lyon and discovered a whole new world: Linux. In september I bought the HP laptop I am currently using and installed an Ubuntu on it. I then learned a few new languages: OCaml, C, C++, a little bit of Python, PHP. I started using vim as my text editor, ion3 as my window manager and became a CLI fanatic. I got contaminated by the dual screen sickness7.

In June 2007 I abandoned my personal blog and decided to have a more technical one, this was the birth of Syntactic Sugar8. I discovered Duels and launched what would be its unique fansite for 6 months. In October I finally switched to Debian and installed a server in my studio, in order to have all my web development, a few screens, etc. permanently available. In December I fell in love with JavaScript and prototype based programming. More recently, I translated this whole blog in English, and here we are.

That's all for now. I can't really guarantee that the timeline is exact, but it seems that the dates are correct9. I'll try to post a follow up in 10 years :D

  1. which is the best solo game I ever played []
  2. I don't remember the name, it featured a blue wizard and some kind of light beams… []
  3. this is still true []
  4. G6-200 []
  5. shame on me… []
  6. The Fourth Coming []
  7. I haven't recovered yet []
  8. I should write about this name, someday []
  9. as far as I can trust my own memory []