Browser Add-ons, JavaScript, Netscape Navigator, Programming

Extending JavaScript

There are a lot of common programming algorithms that are built into various languages as functions: checking if an array contains a certain item, trimming a string, mapping a function to an array, etc. JavaScript doesn’t have a lot of these built-ins, but with prototypical inheritance, it’s easy enough to add them.

I’m thinking it might be beneficial to include with the browser a “JavaScript extension” file – that is, one JS file packaged with the browser that contains function definitions for these common tasks. It wouldn’t necessarily be visible to scripts in webpages (and it would probably be a bad idea to do so, as it might break current scripts), but it could be utilized by extension developers if they choose to do so, just by adding this to their main XUL overlay file:

<script type="application/x-javascript" src="chrome://browser/content/extend.js"></script>

I know that I’ve had to write an Array.contains function for just about every extension I’ve written, so I know this kind of library would come in handy. These are the functions that got me thinking about this:

String.reverse
String.trim (and maybe ltrim and rtrim, but these tend to be used less)
String.md5
String.stripTags (not as keen on this one)
Array.contains
Array.map
Array.random
Array.shuffle
Date.format (a pseudo-port of PHP’s date() function)

Any recommendations for other functions that might as well be standard?

Note: As this is being posted on my personal site, this is no guarantee or indication that Netscape Navigator 9 will be supplying this kind of library. If the feedback is positive though, I would definitely bring it up for consideration.

Standard

Leave a Reply

Your email address will not be published. Required fields are marked *