ECMAScript5 Features - Strict Mode, JSON And More

Introduction

There are several other new features and APIs that need attention, as well, the largest of which are Strict Mode and native JSON support.

Strict Mode

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This strict context prevents certain actions from being taken and throws more exceptions (generally providing the user with more information and a tapered-down coding experience).

Since ECMAScript 5 is backwards-compatible with ECMAScript 3, all the “features” that were in ECMAScript 3 that were “deprecated” are just disabled (or throw errors) in strict mode, instead.

Strict mode helps out in a couple ways:

  • It catches some common coding bloopers, throwing exceptions.
  • It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).
  • It disables features that are confusing or poorly thought out.

Most of the information about Strict mode can be found in the ES5 specification [PDF] on page #235.

It should be noted that ECMAScript 5’s Strict mode is different from the Strict mode available in Firefox (which can be turned on by going to about: config and enabled javascript.options.strict). ES5’s strict mode complains about a completely distinct set of potential errors (whereas Firefox’s existing strict mode tries to enforce some good practices, only).

How do you enable strict mode?

Simple. Toss this at the top of a program to enable it for the whole script:

"use strict";

Or place it within a function to turn on strict mode only within that context.

  1. function imStrict(){  
  2. "use strict";  
  3. // ... your code ...  
  4. }  

Note the syntax that’s used to enable strict mode (I love this!). It’s simply a string in a single statement that happens to contain the contents “use strict”. No new syntax is introduced to enable strict mode. This is huge. This means that you can turn strict mode on in your scripts – today – and it’ll have, at worst, no side effect in old browsers.

As you may note from the examples here and ECMAScript4 there are virtually no new syntax additions or changes to the language in ECMAScript 5. This means that you can write your ES5 scripts in a manner that will be able to gracefully degrade for older useragents – something that wasn’t possible with ECMAScript 4. The way in which strict mode is enabled is a great illustration of that point in practice.

A neat aspect of being able to define strict mode within a function is that you can now define complete JavaScript libraries in a strict manner without affecting outside code.

  1. // Non-strict code...  
  2. (function(){  
  3. "use strict";  
  4.    
  5. // Define your library strictly...  
  6. })();  
  7. // Non-strict code... 

A number of libraries already use the above technique (wrapping the whole library with an anonymous self-executing function) and they will be able to take advantage of strict mode very easily.

So what changes when you put a script into strict mode? Several things.

Variables and Properties

An attempt to assign foo = “bar”; where ‘foo’ hasn’t been defined will fail. Previously it would assign the value to the foo property of the global object (e.g. window.foo), now it just throws an exception. This is going to catch some annoying bugs.

Any attempts to write to a property whose writable attribute is set to false, delete a property whose configurable attribute is set to false, or add a property to an object whose extensible attribute is set to false will result in an error. Traditionally no error will be thrown when any of these actions are attempted, it will just fail silently.

Deleting a variable, a function, or an argument will result in an error.

  1. var foo = "test";  
  2. function test(){}  
  3. delete foo; // Error  
  4. delete test; // Error  
  5. function test2(arg) {  
  6. delete arg; // Error  
  7. }  

Defining a property more than once in an object literal will cause an exception to be thrown

  1. // Error  
  2. { foo: true, foo: false }  

eval

Virtually any attempt to use the name ‘eval’ is prohibited – as is the ability to assign the eval function to a variable or a property of an object.

  1. // All generate errors...  
  2. obj.eval = ...  
  3. obj.foo = eval;  
  4. var eval = ...;  
  5. for ( var eval in ... ) {}  
  6. function eval(){}  
  7. function test(eval){}  
  8. function(eval){}  
  9. new Function("eval")  

Additionally, attempts to introduce new variables through an eval will be blocked.

  1. eval("var a = false;");   
  2. print( typeof a ); // #ff0000  

Functions

Attempting to overwrite the arguments object will result in an error:

  1. arguments = [...]; // not allowed  

 

Defining identically-named arguments will result in an error function( foo, foo ) {}.

Access to arguments.caller and arguments.callee now throw an exception. Thus any anonymous functions that you want to reference will need to be named, like so,

  1. setTimeout(function later(){  
  2. // do stuff...  
  3. setTimeout( later, 1000 );  
  4. }, 1000 );  

The arguments and caller properties of other functions no longer exist – and the ability to define them is prohibited.

  1. function test(){  
  2.   function inner(){  
  3.       // Don't exist, either  
  4.       test.arguments = ...; // Error  
  5.       inner.caller = ...; // Error  
  6.   }  
  7. }  

Finally, a long-standing (and very annoying) bug has been resolved: Cases where null or undefined is coerced into becoming the global object. Strict mode now prevents this from happening and throws an exception instead.

  1. (function(){ ... }).call( null ); // Exception  

 

with(){}

with(){} statements are dead when strict mode is enabled – in fact it even appears as a syntax error. While the feature was certainly mis-understood and possibly mis-used I’m not convinced that it’s enough to be stricken from the record.

The changes made in ECMAScript 5 strict mode are certainly varied (ranging from imposing stylistic preferences, like removing with statements, to fixing legitimately bad language bugs, like the ability to redefine properties in object literals). It’ll be interesting to see how people begin to adopt these points and how it’ll change JavaScript development.

All that being said, I’m fairly certain that jQuery is ES5-Strict compatible right now. Once an implementation of the language is made available (so that that premise may be tested) I’ll happily switch jQuery over to working exclusively in strict mode.

JSON

The second major feature of the language is the addition of native JSON support to the language.

I’ve been championing this move for a long time and I’m glad to see it finally arrive in a specification.

In the meantime, PLEASE start migrating your JSON-using applications over to Crockford’s json2.js. It is fully compatible with the ECMAScript 5 specification and gracefully degrades if a native (faster!) implementation exists.

In fact, I just landed a change in jQuery yesterday that utilizes the JSON.parse method if it exists, now that it has been completely specified.

There are two primary methods for handling JSON: JSON.parse (which converts a JSON string into a JavaScript object) and JSON.stringify (which convert a JavaScript object into a serialized string).

JSON.parse(text)

Converts a serialized JSON string into a JavaScript object.

  1. var obj = JSON.parse('{"name":"John"}');  
  2.    
  3. // Prints 'John'  
  4. print( obj.name );  
  5. JSON.parse( text, translate )  

Use a translation function to convert values or remove them entirely.

  1. function translate(key, value) {  
  2.   if ( key === "name" ) {  
  3.        return value + " Resig";  
  4.   }  
  5. }  
  6. var obj = JSON.parse('{"name":"John","last":"Resig"}', translate);  
  7. // Prints 'John Resig'  
  8. print( obj.name );  
  9. // Undefined  
  10. print( obj.last );  
  11. JSON.stringify( obj )  

Convert an object into a serialized JSON string.

  1. var str = JSON.stringify({ name: "John" });  
  2. // Prints {"name":"John"}  
  3. print( str );  
  4.    
  5. JSON.stringify( obj, ["white""list"])  

Serialize only a specific white list of properties.

  1. var list = ["name"];  
  2.    
  3. var str = JSON.stringify({name: "John", last: "Resig"}, list);  
  4. // Prints {"name":"John"}  
  5. print( str );  
  6.    
  7. JSON.stringify( obj, translate )  

Serializes the object using a translation function.

  1. function translate(key, value) {  
  2.    if ( key === "name" ) {  
  3.        return value + " Resig";  
  4.    }  
  5. }  
  6. var str = JSON.stringify({"name":"John","last":"Resig"}, translate);  
  7. // Prints {"name":"John Resig"}  
  8. print( str );  
  9.    
  10. JSON.stringify( obj, null, 2 )  

Adds the specified number of spaces to the output, printing it evenly.

  1. var str = JSON.stringify({ name: "John" }, null, 2);  
  2.    
  3. // Prints:  
  4. // {  
  5. //   "name": "John"  
  6. // }  
  7. print( str );  
  8. JSON.stringify( obj, null"\t" )  

Uses the specified string to do the spacing.

  1. var str = JSON.stringify({ name: "John" }, null"\t");  
  2.    
  3. // Prints:  
  4. // {\n\t"name": "John"\n}  
  5. print( str );  

Additionally, a few new generic methods have been added to some of the base objects but, frankly, they aren’t that interesting. The results from String, Boolean, and Number are just equivalent to calling .valueOf() and the result from Date is equivalent to calling .toISOString()

  1. // Yawn...  
  2. String.prototype.toJSON  
  3. Boolean.prototype.toJSON  
  4. Number.prototype.toJSON  
  5. Date.prototype.toJSON  

.bind()

A welcome addition to the language is a built-in .bind() method for enforcing the context of a function (virtually identical to Prototype’s .bind implementation).

  1. Function.prototype.bind(thisArg, arg1, arg2....)  

 

Enforces the ‘this’ of the specified function to a specific object – and passing in any specified arguments.

  1. var obj = {  
  2.    method: function(name){  
  3.        this.name = name;  
  4.    }  
  5. };  
  6. setTimeout( obj.method.bind(obj, "John"), 100 );  

Considering how long this function (and its equivalents) have been around it’s a welcome addition to the language.

Date

Dates are now capable of both parsing and outputting ISO-formatted dates. Thank goodness, about time.

It now attempts to parse the date as if it was ISO-formatted, first, then moves on to the other inputs that it accepts.

Additionally, date objects now have a new .toISOString() method that outputs the date in an ISO format.

  1. var date = new Date("2009-05-21T16:06:05.000Z");  
  2.    
  3. // Prints 2009-05-21T16:06:05.000Z  
  4. print( date.toISOString() );  

.trim()

A native, built-in, .trim() is now included for strings which works identically to all the other trim methods out there – with the potential to possibly work faster.

Array

The JavaScript Array Extras that’ve been around for, what seems like, forever are finally formally specified. This includes the following methods: indexOf, lastIndexOf, every, some, forEach, map, filter, reduce and reduceRight.

Additionally, a new Array.isArray method is included, providing functionality very similar to the following:

  1. Array.isArray = function( array ) {  
  2.      return Object.prototype.toString.call( array ) === "[object Array]";  
  3. };  

Conclusion

Altogether, I think ECMAScript5 makes for an interesting package. It isn’t the massive leap that ECMAScript 4 promised but it is a series of respectable improvements, that reduces the number of obvious bugs while making the language safer and faster.

Up Next
    Ebook Download
    View all
    Learn
    View all