Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
haeuptle
Advisor
Advisor

JavaScript has many pitfalls. In 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’.


When you start a new application, you can switch on strict mode from the beginning.

For more information about strict mode see:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Stric...

You should, however, be careful when using strict mode in applications with an existing codebase. 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 post to 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

1 Comment