JavaScript has many pitfalls. With strict mode you can detect some bugs earlier. Strict mode makes several changes to normal JavaScript semantics:
- First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. E.g. makes it impossible to accidentally create global variables. In normal JavaScript mistyping a variable in an assignment creates a new property on the global object and continues to "work".
- Second, strict mode prohibits the use of keywords which are likely to be defined in future versions of ECMAScript, such as ‘implements’, ‘private’, and ‘protected’.
If you start a new application, you can switch on strict mode from the beginning.
For more information about strict mode see:
But applications with an existing codebase you should be careful. Since some of the errors are only detected during runtime, switching to strict mode for your existing productive code without testing everything can be risky. If you cannot ensure all code is touched by automated tests, you might not want to enable strict mode for your productive code. Instead you can use the strict mode for executing your unit tests to detect some bugs earlier without putting strict mode in the productive implementation.
How to? You can use a self-executing anonymous function to put your test in strict mode. Douglas Crockford describes the reasons for using the self-executing function in his blog post: http://www.yuiblog.com/blog/2010/12/14/strict-mode-is-coming-to-town/
(function() {
"use strict";
module( "Assign List", {
setup : function() {
},
});
test(){
}
..
}());
This blog post is part of a series,like the following blog postto stay tuned and get updates about more topics around software engineering with SAPUI5 and JavaScript:
http://scn.sap.com/community/developer-center/front-end/blog/2013/12/12/engineering-in-javascript