Updated JavaScript and you
Posted: Thu Jan 06, 2011 1:57 am
Oolite nightlies are now using an updated JavaScript engine which supports ECMAScript Fifth Edition. Here’s an overview of new features:
Strict mode
Strict mode causes certain bad behaviours to be treated as errors, and potentially lets scripts run a bit faster. To activate strict mode, put the string
Many of the things that are not allowed in strict mode were already warnings because Oolite ran SpiderMonkey in, er, strict mode. But a different strict mode. For Oolite’s purposes, this SpiderMonkey-specific strict mode has now been renamed “pedantic mode”.
You can read more about strict mode on the interwebs, for instance here. I recommend using it, at least for new scripts. All Oolite’s built-in scripts now do.
JSON
ECMAScript 5 has build-in support for JSON, a common format for representing structures as strings – similar to plists. The obvious use case in Oolite is that it allows you to store structured data in mission variables.
Array and string extras
A number of higher-order functions for array processing are now standardised. These methods were already supported in Oolite, except for reduce() and reduceRight().
A new function
A new string method
Function.prototype.bind()
Already common in web JS libraries,
This is mostly useful with callback APIs that don’t take a
Object “meta-control”
The new object and property configuration methods are good additions, but rather abstract, and I’m not going to try to describe them at this time of night. They’re mostly of interest for writing libraries, and if you’re doing that you hopefully don’t get all your JavaScript information from me.
Moar faster
Not actually an ECMAScript 5 feature, the new version of SpiderMonkey includes not one but three JIT compilers¹ – TraceMonkey, JägerMonkey and YARR – and is competitive with the two WebKit JavaScript engines (SquirrelFish and V8) which have been dominating the ongoing speed war for the last few years. In updating to the new engine, several speed improvements have been made in Oolite’s JavaScript-related code as well.
¹However, only one (TraceMonkey) works on PowerPC systems.
Strict mode
Strict mode causes certain bad behaviours to be treated as errors, and potentially lets scripts run a bit faster. To activate strict mode, put the string
"use strict"
as a statement by itself at the start of your script, or at the start of a function body to use it only in that function:Code: Select all
this.foo = function ()
{
"use strict"; // You are now entering the strict zone.
var x;
delete x; // Exception in strict mode, warning otherwise.
}
You can read more about strict mode on the interwebs, for instance here. I recommend using it, at least for new scripts. All Oolite’s built-in scripts now do.
JSON
ECMAScript 5 has build-in support for JSON, a common format for representing structures as strings – similar to plists. The obvious use case in Oolite is that it allows you to store structured data in mission variables.
Code: Select all
this.$settings = JSON.parse(missionVariables.mySettings);
if ($settings == null) $settings =
{
aString: "Frederick",
aNumber: 73,
anObject: { stuff: [ "banana", {}, false ] }
};
this.$saveSettings = function ()
{
missionVariables.mySettings = JSON.stringify($settings);
}
A number of higher-order functions for array processing are now standardised. These methods were already supported in Oolite, except for reduce() and reduceRight().
A new function
Array.isArray()
reliably tests whether an object is a standard array. Note that this is not a method on the object being tested; to see if [i]foo[/i]
is an array, call Array.isArray([i]foo[/i]), not [i]foo[/i].isArray()
.A new string method
trim()
deletes whitespace at the beginning and end of strings, so " banana ".trim()
becomes "banana"
. This is already supported in Oolite 1.74 through the global prefix script.Function.prototype.bind()
Already common in web JS libraries,
bind()
is a method on functions that defines their this
value:
Code: Select all
var foo =
{
x: 5,
f: function () { return x + 1; }
}
var f1 = foo.f;
f1(); // Error: x is undefined.
var f2 = foo.f.bind(foo);
f2(); // Works.
this
parameter. All of Oolite’s do, so if this just looks confusing you can ignore it.Object “meta-control”
The new object and property configuration methods are good additions, but rather abstract, and I’m not going to try to describe them at this time of night. They’re mostly of interest for writing libraries, and if you’re doing that you hopefully don’t get all your JavaScript information from me.
Moar faster
Not actually an ECMAScript 5 feature, the new version of SpiderMonkey includes not one but three JIT compilers¹ – TraceMonkey, JägerMonkey and YARR – and is competitive with the two WebKit JavaScript engines (SquirrelFish and V8) which have been dominating the ongoing speed war for the last few years. In updating to the new engine, several speed improvements have been made in Oolite’s JavaScript-related code as well.
¹However, only one (TraceMonkey) works on PowerPC systems.