Drawing Hands by M.C. Escher (1948)

Jeff Mesnil

Archive for the ‘javascript’ Category


Updated split bookmarklets

On August 4th, 2008 in javascript (No Comments »)

I was doing some house-cleaning on my weblog after upgrading Wordpress and redesigning its presentation (in a minimalist but not so minimalist fashion) when I saw that my popular split bookmarklets were broken due to a bad formatting.

I have fixed them and checked that they work correctly with Firefox 3, Safari 3 and WebKit nightly builds (my preferred browser these days) but I have not checked if they work on IE and Opera. If you can test on these browsers, let me know if they work.

Review of JavaScript: The Good Parts

On May 27th, 2008 in book, javascript (No Comments »)

I admit: I don’t like JavaScript.

I have written and maintained very few scripts and it was a frustrating experience to have them working on several browsers.
However this was many moons ago and I wanted to reevaluate JavaScript after using it in unexpected places (i.e. outside of the browser) like in CouchDB View API or Eclipse Monkey (I already wrote about JMX scripts using Eclipse Monkey).

I was looking for a good book on JavaScript, the language. There are many many books on JavaScript but they focus mainly on JavaScript in the browser and spend thousands pages describing the DOM (please, Messrs. the editors, save the Amazon forest and just print some links to the online DOM documentation…).
I wanted a concise book about writing simple and maintainable code. I also wanted to learn more about the weird syntax constructions spotted when reading non-trivial bits of code, such as processing.js or CouchDB View’s map/reduce.

I bought “JavaScript: The Good Parts” on the strength of the author’s chapter in “Beautiful Code” and I was not disappointed.
This is exactly the kind of book I’m looking forward to when learning a programming language. It is short (100 pages + 50 pages of appendixes including JSON description) but dense, the sample code are small and meaningful (even the done to death fibonacci and factorial functions used here to explain memoization).
The book does not lose space describing extensively the whole language. It focuses on the subset which is good and proven and do not talk about the edges or the parts which are better forgotten. It really shows how to write code which is both readable, maintainable and elegant.
I’m sure I’ll come back to this book every time I read JavaScript code using some peculiarities of the language that I don’t understand.

After reading this book, I’ve got a better understanding of JavaScript and now sees the good (and even beautiful) parts of it. I have a better appreciation for its prototype-based design even if it is hindered by a class-based syntax.
I also find it frustrating that the JavaScript standard library is so useless (no I/O to communicate with the rest of the world). Of course, the library is richer when the code is to run in the browser or on Rhino (gaining access to the whole Java platform) but, by itself, the standard library is very poor compared to what comes bundled with Python or Ruby.

These are critics of the language. For the book, I’ve got nothing but praises. I recommend it to any programmer wanting to learn more about what is good in JavaScript.

One advice about the code examples: it is much simpler to use Spidermonkey or Rhino than the web browser to run the different scripts and experiment with them interactively.

How to include JavaScript engine in Apple’s Java 6 VM

On May 14th, 2008 in apple, java, javascript, macosx (1 Comment »)

After complaining in my previous post, here is a more constructive guide to use JavaScript with Apple’s Java 6 VM:

  1. Download JSR 223’s engines
  2. Copy jsr223-engines/javascript/build/js-engine.jar to /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/
  3. Download Rhino
  4. Copy rhino1_7R1/js.jar to /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/

You can now use a “JavaScript” engine from Apple’s Java 6 VM:

public class JavaScriptTest {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        engine.eval("print('hello, world!')");
    }
}

jrunscript is also working:

    $ cd /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/
    $ ./jrunscript 
    rhino-nonjdk> print("hello, world");
    hello, world
    rhino-nonjdk>

No JavaScript in Java 6 on Mac OS X

On May 14th, 2008 in apple, java, javascript, macosx (1 Comment »)

I had an idea about using JavaScript from Java 6 and I wanted to give it a try on my MacBook.
No such luck: Apple has recently released Java 6 for Mac OS X Leopard but somehow it does not include Rhino, the Mozilla’s JavaScript engine bundled in Sun Java 6 release.

Instead they provided only one engine for AppleScript but frankly:

public class AppleScriptTest {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("AppleScript");
        engine.eval("tell application \"Finder\"\n display dialog \"Who cares?\"\n end tell");
    }
}

Nuff said…

Updated Split bookmarklets (works on IE6)

On July 25th, 2004 in javascript, misc (4 Comments »)

Thanks to the many comments I had about the split bookmarklets, I updated them and here they are:

the main changes are:

I’m by no means a JavaScript wizard but I find quite amazing the difficulties to get such a simple javascript code to work on different browsers. With that little experience, I’m even more impressed by web applications using JavaScript like Gmail (Oddpost is also worth to take a look but they made their job easier by only targeting Internet Explorer).