Question to JavaScript gurus: how would i know if my code works in various browserless environments like Reeno and V8? Is there a compatibility table of various JS features is isn't browser-centric?
Don't down vote this fellow's question, its perfectly valid and interesting.
First, you have to make a distinction between Javascript(ECMAScript) the language, and the API's that an environment makes available to it.
A good example of this is document.getElementById(). This is universally found in browsers, but you won't find this in Node.js (a server-side javascript environment using V8). Conversely, Node makes available Buffers which you won't find in any browser.
I think the best way to answer your question is to identify in the respective environments which version of ECMAScript they claim to support. Various versions of each browser support different versions of ECMAScript, or such as Internet Explorer where earlier versions supported a variant called JScript that didn't always tie directly to the published standards.
Get "JavaScript: The Definitive Guide" and look at the API reference, it's split into two sections, one for APIs provided with every JavaScript implemetation, and one for APIs provided by browsers.
Currently there's not much compatibility between server-side JavaScript platforms (beyond the minimal set of APIs provided by the language implementations), something we're trying to solve with CommonJS, but that effort has been inhibited by node.js's sudden rise in popularity and refusal to work with CommonJS...
The best option is to avoid everything that is not part ECMAScript 262, and even programming according to the spec you maybe at some point some inconsistencies.
Check out the sputnik test project[1] to see what is working in the different VM.
An easy example of inconsistency :
If you read the specs you will find that FunctionDeclarations are only allowed to appear in Program or FunctionBody. Syntactically, they can not appear in Block
s. But because the specs allowed extensions you can find that Mozilla/rhino/spidermonkey implement a different way that Webkit/V8
run this code in firebug and in the inspector a you will see that produce two difference.
<code><pre>
if (true) {
function foo(){ return 1; }
}
else {
function foo(){ return 2; }
}
foo();
</pre><code>
Indeed things are not that bad, because is easy to avoid it.
var foo;
if (true) {
foo = function foo(){ return 1; };
}
else {
foo = function foo() { return 2; };
}
[1]sputnik.googlelabs.com/
you can read, re-read, etc. the spec to get a good feel of the language
but at the end of the day, some hosts could implement things a bit differently,
especially on the "blurry" parts of the spec.
For example, two engines could support regexp but have a slightly different implementation
and be both valid ECMA-262 edition 3, idem for number precisions, etc.
So if your main concern is to avoid browser-centric JS features, test with the engine of those browsers
http://en.wikipedia.org/wiki/JavaScript_engine
* Chrome => V8
* Firefox => SpiderMonkey / TraceMonkey
* Internet Explorer => IE JScript / Chakra
* Safari => JavaScriptCore / SquirrelFish
* etc.
and simply put, you can run your JS code base on those tools
to find the common denominator and/or limitations, imho
it is simpler to run tests on the command line than in the browser itself.
If already your library work with all the engines you're in pretty good shape.
Another way to be in "good shape" is to simply re-use one of the popular
JS libraries, like JQuery, as those are pretty well tested.
But to be honest it depends on how far you want to go,
most likely testing only the JS engine will not be enough if you deal a lot
with the browser DOM, you will have to test in the browsers anyway,
and there that's where the fun begin, because as much as you can find slight difference
of implementations in JS engine, you can find bigger differences in browser implementations,
yep even if all that is supposed to be "standard".
Thanks!