I don’t mind if I’m incomprehensible

Posts Tagged ‘programming

Installing and Using Google V8’s sample shell

leave a comment »

The sample shell that comes with Google’s V8 javascript engine is pretty nice for playing with naked, DOM-free javascript.  Here are some notes to augment Google’s download and build documentation.

If you are building on a Mac, download and install Xcode with the the Unix development tools option selected.  This will take care of the compiler and libraries required for the build.

I symlinked the shell to /usr/local/bin/v8 for easy access.  v8 below refers to that, not the project name or the project source directory.

Naturally, typing in v8 starts the interactive shell. v8 --shell does the same thing.

v8 -e followed by a string of javascript allows you to execute that piece of code.

v8 followed by one or more file paths tells the shell to load and run the javascript files in the given order. Now if you add the aforementioned --shell, the sample shell will load and execute the file(s) and then starts the interactive shell.

You could also load files while in the shell by calling the load function. For example, load('hello-world.js'); will load and execute the file hello-world.js from the current working directory. Since the sample shell provides the load function, you could also use it in your javascript code. So instead of typing in v8 followed by over 9000 file paths, you could specify a file that in turn loads the other necessary files.

Another function the sample shell provides is print. That one’s self-explanatory.

There is also a read function, which reads a file into a javascript string.

If you know of a more advanced V8 shell, please leave a comment.


Written by Barry

April 3, 2010 at 8:12 pm

Posted in Nerdy

Tagged with , ,

JavaScript Closures (a fairly simple explanation of)

with 3 comments

I had a job interview recently.  I started the day by missing a flight, and then it was mostly downhill from there.  However, I did pretty much nailed a JavaScript question where the solution involved using closures.  The interviewer sounded surprised that I gave the solution; I was surprised at his reaction because I thought higher order programming, anonymous functions, and closures are common knowledge in JavaScript programming.

Below is my attempt at an explanation of what was used in the solution.  Hopefully it will help someone in an interview.

In JavaScript, you could define an anonymous function and immediately execute it:

(function(){ alert('zombies!'); }())

The wrapping parentheses are necessary here to turn that piece of code into an expression.  Otherwise you’d have a function declaration without a function name, and the JavaScript console will say something about a syntax error.  Alternatively, you could also write

(function(){ alert('zombies!'); })()


var testo = function(){ alert('zombies!'); }();

In the last example, we could leave out the parentheses because the code is in an assignment expression.

Of course, a function can accept arguments, so now let us do

(function(thing){ alert(thing); })('zombies!');

Same thing, but more work.

JavaScript functions can also create and return functions.  That means we can rock

(function(thing){ return function(){alert(thing);} })('zombies!')();

That’s even more work, with more parentheses.  In the above example, we create a function and called it with the string ‘zombie!’ as the argument.  That (outer) function then creates a function and return it, then we immediately call it with that last pair of parentheses.

To make it (slightly) more readable

var testo = function(thing){ return function(){alert(thing);} })('zombies!');

testo is the function returned by the outer function. And it always alerts about zombies. That’s the closure bit. The string “zombies!” has been closed over in the returned function.

One instance where this is useful is when you need the value of a variable that’s being updated, let’s say, in a loop. For example

var things = ['ghosts!', 'ghouls!', 'vampires!', 'zombies!'];
var alerts = {};
for (var i in things) {
    alerts[things[i]] = function(thing) { return function(){ alert(thing); }; }(things[i]);


There we go, closures.

Written by Barry

March 13, 2010 at 4:52 pm

Posted in Nerdy

Tagged with ,