What is JavaScript?

I haven’t updated my site in a while because my wife and I have been very busy moving from the DC-Metro area to Seattle, WA. We made the trip in a five-day drive.

We stopped at a coffee shop in Cour d’Alene, Idaho along the way, and I heard two guys talking about JavaScript. Here is the gist of the conversation:

-“I can’t really find any reasonable documentation for JavaScript’s API”
-”I could be way off, but doesn’t jQuery fit in there somewhere?”
-”jQuery is a framework for JavaScript, but there isn’t any *real* documentation for JavaScript’s functions and objects. I search and search, and all I find is blog after blog.”

So, I’ve decided to blog about JavaScript!

I didn’t butt into the coffee shop conversation and tell the gentleman that JavaScript is an ECMAScript implementation that is largely reliant on the browser within which it is implemented. The reason I didn’t say this is because it’s a common misconception that JavaScript is used solely within a browser’s rendered page.

For instance, Google coded a JavaScript engine called v8 (ECMA-262-compliant and from scratch, as I understand it) which “can run standalone, or can be embedded into any C++ application.” (source: http://code.google.com/p/v8/ ) This means that a developer could write code in JavaScript and run it as any scripting language (ruby, php, bash, etc.) This alone is a difficult concept to grasp for those of us who immediately jump to “JavaScript depends on whatever browser…” answers.

The API itself is a standard released by ECMA International. In short, ECMA International was created to standardize certain languages and technologies and influence the community to adopt these standards. Languages such as JavaScript, C#, and C++ follow ECMA standards. More information can be found on their website: http://www.ecma-international.org/

I wanted to explore the standalone side of JavaScript scripting, so I did a little experiment with the v8 source code (which, unfortunately, you’ll have to download and build from scratch).

Downloading and building v8 (on openSuSE 11.3 x64)

Install Prerequisites and checkout v8

sudo zypper install subversion
sudo zypper install python
sudo zypper install scons

cd ~/bin && mkdir v8 && cd v8
svn checkout http://v8.googlecode.com/svn/trunk/ v8-read-only
cd v8-read-only

Options and building.

Open SConstruct and view all options. A good number of options are displayed as values of hashes similar to JSON key/value pairs (e.g. all:”mode:debug”) and can be written on the command line replacing the colon with an equal sign (e.g. mode=debug). This is necessary knowledge if buildling on a x64 linux machine because it’s not well-documented on the site.
Now, run the build for a developer shell:

scons d8 arch=x64 snapshot=on mode=debug

Test out the command line scripting

jim@linux-g64g:~/bin/v8/v8-read-only> ./d8_g 
V8 version 2.4.6 [console: dumb]
d8> var obj = {};
d8> obj.FirstName = "Jim";
d8> obj.LastName = "Schubert";
d8> function foo(o) { print("My name is " + o.FirstName + " " + o.LastName); }
d8> foo(obj);
My name is Jim Schubert

I’ve only been playing around with this for a few minutes now, and I’m wondering how powerful of a scripting language JavaScript can be using a standalone v8 interpreter. After a little more research regarding server-side JavaScript, I found a CGI wrapper around v8 which is also hosted on Google Code.

Flattr this!