Dynamic function names in JavaScript

Creating a function with a custom name is not something you can do easily out of the box in ECMAScript. Thankfully, ECMAScript comes with a custom function constructor.

First, the basic code, which will give most of you want you want:

The above will create an anonymous function, which when called creates the named function (using the name variable). This functionality is a good substitute for when you can’t use eval() but you need a function with a custom name. Eval is generally useless in ES5 strict mode for a number of good reasons, so its best avoided.

However, an issue with the code above is that we had to write the main code into a string. This sucks because it makes it difficult to debug (i.e., we can’t easily set break points), so we want to avoid that as much as possible. The solution is to make the main function external, pass it as an argument to the Function constructor to call it.

Another advantage here is that we can set private variables inside our custom function, which can then be mixed with public arguments. Check this out!:

You can basically the start doing cool things from there, like:

The above is mega useful for dynamically implementing custom DOM-like interfaces… like shims. Enjoy! 🙂

W3C Workshop on The Future of Off-line Web Applications

The W3C and Vodafone are hosting a Workshop on The Future of Off-line Web Applications on 5 November 2011, in Redwood City, California. According to the workshop website,

The goal of this workshop is to identify a clear path forward for innovation in the Open Web Platform related to offline Web application invocation and use.

As I’ve done for previous events, I’ve prepared a paper entitled “Misconceptions about W3C Widgets” (PDF, I know… I’ll publish it here in HTMLs when I get some time).

As I am on the program committee, it means I get to review papers. I’ve actually read all the papers that have come in thus far, and it looks like it’s going to be fun workshop. The other program committee members have been a bit slack, however. I’ve only seem papers from about 2 or 3 of them. I hope Microsoft, Google, and Mozilla submit something.

What usually happens after these workshops is that a new Working Group is established. This will probably either mean:

    • The death of W3C Widgets: Google and Moz will make a powerplay and dump in their own JSON based widget format on the w3c (Moz’s offer, Google’s offer).
    • Or,  the rebirth of W3C Widgets: Google and Moz will come to their senses and finally embrace the W3C widget format (unlikely, but here’s to hoping:)).

Arduino + Java = error “Serial Port Already in Use”

If you try to run the Arduino IDE and the RXTX Java library on MacOS Lion and you are hitting a “Serial Port Already in Use” error,  try:

$ sudo mkdir /var/lock
$ sudo chmod 777 /var/lock

That should hopefully allow you to run them both (though only one at a time!)

If you want  interface your Arduino with the Java, see the Arduino documentation.