Tuesday, October 12, 2010

fn with no globals

This is a JavaScript sketch, please read the disclaimers.


To get some decent code reuse, we need baseline support for loading code across environments. CommonJS made a good first pass with the Modules and Packages proposals. The Module one does not work well in the browser though, so I am hoping the Async Module proposal gains traction. Douglas Crockford likes async code loading too (around 39:10 in the video).

Relying on a server to generate client-friendly code is not a universal solution, and it is a particularly poor solution for doing mobile web development, where apps can be created with HTML/CSS/JS but run from the mobile device without a server.

RequireJS implements the Async Module proposal. There are some rough spots around the Packages proposal, particularly around mappings, but it is something that can be used, and I'm trying to support some version of packages with RequireJS.

The ECMAScript folks are looking at a Simple Modules spec, but it does not solve any of the problems for me. The problems have been addressed by RequireJS. It would be nice to not to have to include the code for RequireJS in an app, but it works, it is not crazy big, and it can always be pushed down to native environment support after it is broadly in use.

fn with no globals

What I would like to see instead of a Simple Modules is "fn". It is like "function" but it does not have access to the global space. You would use it just like "function" but any variables missing "var" would not be in the global space (could throw an error). If it needs a "use fn" or something like that, OK, but I am not familiar with when "use" strings are needed. Example:

fn hello() {
//the next line would throw
message = 'hello';

//the next line would be an error too
var name = window.name;

return message;
That would give me one of the big things out of Simple Modules that I cannot already have via RequireJS. It would also shorten the traditionally cumbersome "function" word and encourage good coding practice since it does not have access to the global scope. For situations that need global access, fall back to good old "function".

I would not be surprised if this has already been suggested, but as mentioned in my sketches preamble, just jotting down things as they come to me without doing due diligence.


Peter van der Zee said...

Have you taken a look at "use strict" in ecmascript 5? It disallows unspecified globals (although it might not serve your request entirely), as well as a few other "not dones"... :)

James Burke said...

Peter van der Zee: right, but as you indicate it is not complete -- it still allows access to defined globals like window. One of the benefits touted for the Simple Modules proposal was also eliminating access to the global scope.

So fn would be a way to get that behavior without having to opt in to a module, and hopefully encourage better programming by having a shorter name than "function".

I appreciate that the choice of "fn" can be hard given legacy code that may already use that. If there was a way to get into a "reserved word" list as part of "use strict" now though, that would be ideal.

It may be too late for that, and it is hard, naming things. I was just looking for something that was shorter than "function", encouraged good behavior related to globals, and did not use any weird characters.