Settling down and getting naked (javascript)

Ah, the summer busy season – conferences and black belt testing, with impatient customers and the condo board’s irregularly scheduled crisis to boot. I looked at my todo list for the first time in a few months about a week ago. The weekends have been so full that I’ve only gotten back to a little bit of programming just recently.

A while ago, attending one of the JS.chi meetings, I decided that virtually all discussions of Javascript’s object system turn into discussions of how to make it act like a class/instance system. Meanwhile, I’ve used the prototypal inheritance to my advantage (or at least my amusement) on at least two occasions.

One of the things that occurred to me is that I might be able to use Naked Javascript for object diagrams. Better yet, with a little work, I could use it for interactive experiments if there were any questions. Consequently, I’ve spent some of the last week adding long needed features.

System Update

I built NJS on top of bunch of libraries, mainly jQuery and a beta version of jQueryUI. jQueryUI is out of beta and looking a little more polished now (complete with interactive theme designer), although I still ran into some difficulties with dialog placement (fixed in edge) and trickier sizing (not yet evaluated in edge)

Function Execution

The first big thing was adding function execution, which led to most of the other features. I knew the basic strategy was to create an object representing the parameters and then browse that, but of course there were details to work out. One minor hitch is that most system functions don’t have declared parameters. Furthermore, since I don’t have much occasion to use them interactively, I’ve made zero-declared-arguments execute without asking for arguments as an interface optimization.

Scratching an Itch

If a function executes in the computer, and no one receives the value, did it really execute? (Depends on whether it’s a pure function or modified mutable state – but that’s almost a different story.) I do need to display a return value somewhere. At first I made up a one element object to browse, though this is somewhat overkill for simple values. Next up, I created a single scratchpad object. Of course, I almost always immediately browse compound objects, so those get automatically opened. I suppose if one is doing more than my short tests, the scratchpad will eventually get crowded, so I may have to add a way to delete properties.

What a Drag

One thing that was difficult to do was executing functions on complex values. One seemly obvious way to do this to enable drag and drop between objects. jQueryUI’s draggable and droppable behaviors made this relatively easy, although it still took a fair bit of tweaking to get the details sorted out. I also ended up going for the behavior which highlighted a hovered target instead of all eligible targets, because the massive screen refresh of the later during a drag operation would cause a screen blank out, at least on the firefox I using.

The Grapevine

A difficulty with functions that they can potentially change anything. Even editing a simple value can have remote effects thanks to prototypal inheritance. I’ve finally got some code in place to update things when a value changes. Most of the time this devolves to a global update, and possibly multiple updates per object (where an update is replacing some or all of the DOM representing an object from scratch). It works well enough for my limited purposes.

Test Harness

Looking at some possible exploratory work, I added some functions to my test objects for some basic operations: deriving an object and adding a property. I’ve actually got two versions now; I started with methods on a playground object that operated directly on it, but now I’ve also got some methods which operate on arbitrary objects.

Dangerously Fun

Writing this up was slowed significantly by thinking of new things to try out. Since function are objects, can you derive an object from them (yes, but it behaves funny) Adding system methods (i.e., toString) can also be tricky; I modified my property adding methods take both a name and value, rather than just a name to work around that difficulty.

It may have come together just in time – the JS.chi people just sent out a message saying their other talks canceled on short notice.

I haven’t updated the live demo yet, but the new code is in the Naked Javascript BitBucket repository if you are really curious.

Posted Tuesday, September 22nd, 2009 under Devlog.

Comments are closed.