Worst Bits

When any programming language is first designed it is not obvious just what commands will be essential and what will just cause problems. That is because the way the language ends up being used may turn out to be quite different from what those responsible for creating it expected.

With most languages this is resolved by releasing a new version of the language every so often. The bad features are deprecated and new ones to make it easier to do the things that the language is being used for are introduced. Programmers who did use the now deprecated commands now replace them with better code and those commands are then completely dropped from the following version.

JavaScript has its share of bad commands that were unfortunately introduced in the original version. The problem with JavaScript though is that not all of the scripts are written by programmers and many of the scripts not written by programmers use all the worst bits of the language. Despite those being identified as things to avoid those people continue to use them because they don't understand that they shouldn't. With it already having been shown to be pointless to deprecate elements in HTML because people would continue to use them despite their being marked as obsolete and about to be removed, a different approach was selected to try to improve JavaScript.

There are now two versions of JavaScript coexisting -perhaps even used together in the same web page. The original JavaScript still works exactly as it always has and allows those who don't have a clue what they are doing to continue to write code using all the worst bits of the language and create a maintenance nightmare for themselves. The new version of JavaScript is called strict mode JavaScript and is identified by the inclusion of one extra statement either at the top of the script (if it is to apply to all scripts in the page) or at the top of a function (if it is to only apply to that function).

"use strict";

Those browsers that support strict JavaScript interpret that statement as the instruction to interpret the JavaScript within the current scope using the new strict mode rules. Those browsers that don't support strict mode see the command as defining a text string that doesn't get assigned anywhere and so can be discarded. Because strict mode is a subset of the original Javascript, anything that works in strict mode can still run normally in browsers that don't support strict mode.

Where strict mode makes the difference is in those browsers that do support it where the JavaScript needs to comply with stricter rules in order for the script to run at all. Perhaps the most significant of the strict rules is that it requires all variables to be declared. You must include a var statement to declare the variables either in global scope or within the particular function they belong to. In strict mode any reference to a variable that is not declared will cause the script to not run at all. Of course if the script were to be run in a browser that doesn't support strict mode the undeclared variables would be created with global scope but if you make sure to test the script in at least one browser that does support strict mode you make sure that can never occur.

Strict mode also does away with the completely unnecessary with statement which made variable references more ambiguous and also often made the code longer than the alternative (despite it being intended to make the code shorter). It simply turned out that there is a more effective, shorter and far less ambiguous way to achieve the same result making that command completely unnecessary so strict mode does away with it.

Strict mode hasn't gone so far as to remove the eval command. While that command to is completely unnecessary there are too many people using it to do away with it completely. Instead the scoping rules that the command running in strict mode has been greatly restricted so that code inside an eval statement cannot interfere with code outside of it. When used in a way that works under the new rules it will work exactly the same in browsers that don't support strict mode and will provide the same restrictions in all browsers provided that the code actually works in browsers that support strict mode.

By implementing this new mode JavaScript has taken the first step in cleaning up the language. Those using JavaScript properly will convert their code to use strict mode while those creating a mess can continue to create that same mess. Future versions of JavaScript will be able to further modify how strict JavaScript works in order to get rid of more of the bad bits of JavaScript and once all browsers support it will be able to shift even further away from the way that the original JavaScript worked. Over time JavaScript will split into two completely different languages - the original version with all the bad bits that those who don't know what they are doing insist on using, and a much cleaner strict JavaScript that has eliminated all the worst bits and many of the other bad bits of the language and added more useful and flexible commands in their place.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow