Archive for 2006

window.setTimeout (or, Don’t Wait for Fat Inits)

Because Javascript is a lambda-based language that’s designed to be compiled and run within a browser, it has a few unique architectural features that differentiate it from its more traditional C-style cousins. In particular, there’s no sleep function in Javascript.

Read the rest of window.setTimeout (or, Don’t Wait for Fat Inits) »

Javascript Daemon Manager Class

I found that I often have a little DHTML widget or something that might show up multiple times on the same page. If you approach that in an object-oriented way, that means that you’ve got a lot of little instances (daemons) running around handling each one, right? Then, I’d write a “manager” that [...]

Read the rest of Javascript Daemon Manager Class »

Function to spawn arbitrary object with arbitrary argument list in Javascript

Good news, everyone!
This function can be used to create an instance of an arbitrary class, and pass the list of arguments to the class’s constructor.
var spawn = function(cls, args) {
  if(!args instanceof Array) args = [args];
  var f=function(){};
  f.prototype=cls.prototype;
  f.prototype.constructor = cls;
  var obj = new f();
  cls.apply(obj,args);
  return obj;
};
Usage:
var myClass = function(a,b,c){ this.a=a; this.b=b; this.c=c; }
var myInstance = spawn(myClass,[1,2,3]); // same [...]

Read the rest of Function to spawn arbitrary object with arbitrary argument list in Javascript »