Archive for the ‘WILT’ Category

Javascript Inheritance and callSuper

Wednesday, February 8th, 2006

So far I’ve got only 2 problems with the inheritance mechanism from the earlier post.
- it doesn’t allow for multiple inheritance. I’m ok with that…coming from java it is just what I’m used to. However, it doesn’t rule out the possiblility of employing a Class Augmentation approach from being used along with it.
- navigating the prototype chain is tricky. Since the code is ‘resetting’ the constructor property on the prototype…I don’t see a way for program code to traverse the chain (aside from using the non-standard ‘__proto__’ property). This is fine for the code that is aware this is the case…it can look for the ’superClass’ property that the inheritance code is adding…but yeah, thats a ‘gotcha’ for when you are wanting to integrate with another library that wants to (for whatever reason) traverse the chain.

I couldn’t sleep tonight, so I dabbled with adding support for calling overridden superclass methods. It was really more as a fun exercise than anything else as I still don’t see a huge problem with just directly calling the superclass method like so: SuperClass.prototype.someMethod.call(this, arg1, arg2);

So the result is the ability to, from any method (i.e. not the constructor) call this.callSuper(); and it will call the superclasses implementation of the method that you are in! You can also call this.callSuperByName(’methodName’); and it will call the superclasses implementation of the method with the given name. It is intelligent enough to begin searching the prototype chain from the depth that the code that called the ‘callSuper’ method is located.

I’ll post the code soon…need to get ready for work (and I want to make sure it works in more than just one browser :)).

Prototype Chain

Sunday, February 5th, 2006

I’m doing a presentation on OO Javascript to Daugherty developers on the 27th. In preparation I was doing some digging online (mostly to make sure I really DO know what I’m talking about) when I ran into an article that talked about the prototype chain. While I was aware of this…I don’t know if it was ever spelled out for me…it was just…there. So it got me thinking about a different way to handle subclassing.

There are 2 primary forms of subclassing I’ve seen:
- Copy the methods from the SuperClass.prototype to the SubClass.prototype
- Set the SubClass.prototype to a new instance of the SuperClass.

I use the first and it actually works pretty well. The only problems I have with it are that it (probably?) won’t work with the instanceof operator (actually…I’ve never used it. I just learned about it, but I’m guessing it doesnt’ work), and that it gets hairy when you do multiple levels of inheritance and are trying to keep track of the heirarchy (so you can do stuff like super.someMethod();). But honestly…both of those are probably smells anyway. Don’t subclasses always know who theier superclass is anyway? The SubClass code could just say: SuperClass.prototype.blah.call(this, argv);…not ‘pretty’, but it works. The point is, that the subclassing via copying methods from the parent prototype works, but it still has its problems.

The second I’ve never liked. What if the SuperClass constructor needs arguments? What if it does something expensive, or needs the DOM, or any number of problems. The ’solutions’ I’ve seen to this are to add code to the SuperClass constructor to determine if it was called with no args. If it was, then do nothing. But really, this is only feasible for a constructor that takes args…what if it was a no-args, but did something expensive…or assumed some state of the browser that is not true at the time of the ‘extend’ call?

For whatever reason, the article about the prototype chain triggered something and got me thinking about a different approach. My first attempt was to just set the Subclass.prototype.prototype to the SuperClass.prototype. That didn’t work. I needed to know more, so I dug into the ECMA 262 spec…not sure why that hadn’t happened before. The spec talks about an IMPLICIT (i.e. not accessible) ‘[[prototype]]’ reference. In essence, every instance ALWAYS knows what prototype it was created based on. The prototype chain heavily relys on this implicit reference to resolve properties. This means that the prototype instance on the SubClass should really have a reference to the SuperClass.prototype for it to truly take advantage of the OO features that ECMA script is defining.

So, at face value that means setting SubClass.prototype to a new instance of the SuperClass. I still don’t like that. However, this is Javascript…where everything can be dynamic. So a new idea was forming. Why not create a new, in-line subclass of the SuperClass and set the SubClass.prototype to an instance of THAT in-line subclass instead? Here is the idea:


Class.extend = function(SubClass, SuperClass){
var innerSuperClass = function(){};
innerSuperClass.prototype = SuperClass.prototype;
SubClass.prototype = new innerSuperClass();
SubClass.prototype.constructor = SubClass;
}

This approach seems to be the best of both worlds…it builds a nice prototype chain, and does not require the SuperClass to be overly careful about calls to its constructor.

Is there something I’m missing here? Is there something about this approach that is just..bad, that I may have missed. Please let me know. I’m sure I’m not the first to do this. Until hearing otherwise, this is the approach I will be using.

Named Inputs

Friday, January 13th, 2006

So, I’ve found myself leaving the ‘name’ attribute off of form elements recently…mostly due to laziness since I’m not using them (I know, I know…unobtrusive, and accessibility). We’re doing an RIA in a closed environment, so we can make the assumption/requirement that the client has javascript enabled. So, most of our stuff is leaning heavily (not quite obtrusively…but it is required) on javascript…and we use css and behaviour to identify the elements in the page and dwr to communicate back to the server. Works fine…great even. The problem is that it isn’t easy for the testers to test it with their robots. My first reaction is that the robots should be smart enough to find the elements by css (like we do)…or at least if it cannot determine a unique css selector for it, ask the tester to provide one (most appear to be automated, text-based scripts). But then again, this would also say to me than any screen readers would have trouble too. Since we aren’t concerned with accessibility (at least to the level of screen readers), that doesn’t bother me too much. The testing problem really does. So, we’re going to need to (at some point…hopefully soon) go back and name all our form elements (or maybe do something automated).

Anyway this whole post was triggered because I noticed that firefox’s ‘form cache’ feature (where if you refresh a page…not reload…just a simple refresh, then the form keeps the values you had in it…not sure if it happens on a back button use) gets confused when the form elements do not have names. I kept seeing values for field a (user name) showing up in field b (some date field).

Moral of the post: use names in the form elements…even if you dont ‘need’ them.

Gmail as an SMTP server

Thursday, July 7th, 2005

I figured this was possible…now i can Use Gmail as an SMTP server while developing apps with email functionality. Its just good to have while coding on the go.

Colorize in Perl

Thursday, May 26th, 2005

In case you ever need to ‘colorize’ a log file from the command line…a guy at work (if he had a blog I’d link to it…nudge nudge) put this together:

use Term::ANSIColor;

while (<STDIN>)
{
if ($_ =~ /ERROR/) {
$color = "red";
} elsif ($_ =~ /WARN/) {
$color = "magenta";
} elsif ($_ =~ /DEBUG/) {
$color = "green";
} else {
$color = "white";
}
print color(${color});
print "$_";
}

print color("reset");

Nice Bookmarklets

Thursday, April 28th, 2005

Browsing around and found some nice bookmarklets (thanks Ramin for the link and thanks Jesse for the collections).

Gmail Identity Crisis

Thursday, April 28th, 2005

My email address at gmail is of the form <first>.<last> ‘at’ gmail.com. Well, a few weeks ago I got an odd spam message (odd because gmail’s filter didn’t catch it). Upon further inspection, the recipient address (mine) didn’t have the ‘.’ in it at all. Looks like it wasn’t even supposed to come to me. So I tested sending a message to that address: <first><last> ‘at’ gmail.com (without the ‘.’) and lo and behold…the email got to me. So it appears that the ‘.’ is not necessary when sending? I wouldn’t bank on it or publish my email address that way…what would happen if someone actually registered with that name? Would gmail allow it?

Generic (Client-Side) Form Validation

Thursday, April 28th, 2005

Just read a pretty decent article on a generic form validation technique here. I bet it wouldn’t take much to extend the technique to also validate by id for more complex validation (similar to/leveraging commons-validator). It would validate the class first (to make sure it is at least of the right type) then move on to more complicated validation against the id (to handle ranges for example). If you’re in a web framework, you could make it dynamic and load the id-based rules from a generated page, otherwise adding ‘validateX’ methods where ‘X’ is the id of the element you want to do further validation on could suffice.

Konfabulator

Tuesday, April 26th, 2005

I heard about Konfabulator a few years ago when it was super young (like 3.5 years ago or so) through a friend of mine. I thought it was cool, but (as I recall) it only worked on a mac (no windows version). Well, now that I’m getting OS X 10.4, and it has Dashboard (some call it a blatant rip off of Konfabulator), I thought I’d see if there was a windows version of konfabulator…there is.

So I installed it and grabbed some pretty cool widgets and am liking it quite a bit. My only gripe is that there is no ‘hide all widgets’ hot-key. I can, however make all the widgets only visible in the ‘Konspose’ mode by hitting ‘F8′ (very similar to what dashboard and expose does…creates a semi-transparent glass-pane over all your windows and displays all the widgets). The widgets I have are:

  • SimpleCalc - Simple calculator…one text field, you just enter the math expression, hit enter and it solves
  • Binary Widget - A binary clock…just for fun
  • Blogliner - Tells me how many unread posts are in my bloglines account
  • Stickies - Post It Notes…has a cool ‘dock’ feature that slides the notes off the screen when they lose focus (might defeat the purpose a bit…but looks neat).
  • Word of the Day - displays word and definition
  • miniIP - Tells your local AND public facing IP (for all you dynamic IP home - hosters)
  • Free Memory - Displays how much physical memory is available
  • Gas Watch - Tells you where the cheapest gas is for your zip code.
  • MLB Ticker - Gives MLB scores (there are soccer ones too…no football)
  • Memory Monitor - Shows a bar graph of memory used/free and tells you which app is the “Memory Hog”
  • Universal Shipment Tracker - Tracks packages from all the big shippers (I’m expecting some packages :) ).
  • Where Is It - A simple search tool that will hit google and others
  • Analog Clock - Simple analog clock
  • The Weather - Tells you the weather in your city along with a representative graphic.
  • What To Do - Simple todo list
  • Tiger Timer - Tells you how many days until Tiger (OSX 10.4) is released.

There is another one called mini FTP that I’ll install and use at home (at work now) its pretty cool…you configure it with the ftp info and the widget is just a simple little icon that you drag files/folders to and it will ftp them to the server.

I really think it would be neat to have some developer-related widgets that would integrate in with your development tools…making a kind of developer dashboard. I can see widgets like a build manager, a cvs tracker, todo lists, test manager, deployment center, site/app monitors, log readers, doc reference/searches, others…

Core Data similar to ATG Repository

Monday, April 25th, 2005

I dunno how long its been around, but I was looking into some OSX Tiger stuff and stumbled onto Core Data:
http://developer.apple.com/macosx/tiger/coredata.html.
In reading through it it sounds a lot like what is done with “Repository” in ATG. I don’t really like the ‘dynamic bean’ approach in either case…no strong typing and messy mappings to and from the actual data model (pojos). At least they both support mapping to pojos as well.
I like Core Data’s drag and drop interface builder feature. Just drag the component from the diagram to an interface being built and it will create a very simple (but functional) interface for it. ATG has a similar interface for each component…kind of a generic editor that provides a way to manipulate repository items’ attributes (data..rows in db) by providing appropriate gui inputs for the associated types.