Strict JavaScript for NodeJS

This post is mostly a short reminder for myself as it’s contents is not new. As many of you probably know NodeJS runs on top of Google’s V8 engine, the same JavaScript engine used in the Chrome browser. V8 can take quite a few configuration options, try listing them out. I’m going to talk about the --use_strict option. Unfortunately the description you get is quite limited.

node --v8-options | grep strict
	--use_strict (enforce strict mode)

Strict mode is part of ES5 so it’s quite well support in many browsers (except IE9, big surprise) as well as by NodeJS. By specifying it at the NodeJS command line strict mode is enabled for your entire application but it can be enabled on a per-file or function bases as well. Place a Sting literal at the beginning of a file or function to enable it "use strict";. As it’s just a normal String declaration it won’t break old browsers that don’t support strict mode.

//Normal code
var foo = 'bar';
(function(msg){
	"use strict";
	//Strict code
	alert(msg)
	...
})(foo);
//Normal code

Shown here inside a self executing function, this is a good pattern for making strict JavaScript libraries. What does strict mode actually do? It makes JavaScript more grown up and stops you doing bad things in your code.

  • Any attempt to get or modify the global object will result in an error: foo = bar;//Error. No var so foo would get set on the global object.
  • null values of this will no longer be evaluated to the global object and primitive values of this will not be converted to wrapper objects.
  • Writing or deleting properties which have there writeable or configurable attributes set to false will now throw an error instead of failing silently.
  • Adding a property to an object whose extensible attribute is false will also throw an error now.
  • A functions arguments are not writeable so attempting to change them will now throw an error arguments = [...].
  • with(){} statements are gone, if you know what they are you hopefully know why and if you don’t, just pretend you never came across them.
  • Use of eval is effectively banned.
  • eval and arguments are not allowed as variable or function identifiers in any scope.
  • The identifiers implements, interface, let, package, private, protected, public, static and yield are all now reserved for future use (roll on ES6).

To keep JSLint happy you will need to place "use strict"; at the top of every file and when running Node itself you should run with the --use_strict option. This is easy to do with the NPM start script which I hope your using anyway. Change your package.json to contain the following property and start you application with npm start.

"scripts": {
	"start": "node server.js --use_strict"
},

I have kept things short here on purpose, for a more in depth look at strict mode and some new JSON features in ES5 have a look at this excellent post by John Resig, ECMAScript 5 Strict Mode, JSON, and More. I hereby give some (but not all) credit to his post for the content in this one. I think the changes strict mode makes and other changes coming in Harmony ES6 show that JavaScript is growing up and has a bright future, well I’m a fan at least.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.