Openui5: Location of "use strict" in a controller

Created on 24 Oct 2020  路  5Comments  路  Source: SAP/openui5

OpenUI5 version: 1.83.x

In all UI5 documentation I always see "use strict" right after controller's callback, e.g.:

sap.ui.define([
    "sap/m/Shell",
    "sap/ui/core/ComponentContainer"
], (Core, Shell, ComponentContainer) => {
    "use strict";

    new Shell("", {
        app: new ComponentContainer("", {
            height: "100%",
            name: "webapp"
        }),
        appWidthLimited: false
    }).placeAt("content");
});

Questions:

  1. Is it mandatory to place "use strict" inside of a controller's callback?
  2. Why can't I write "use strict" just at the beginning of a controller, e.g.:
"use strict";

sap.ui.define([
    "sap/m/Shell",
    "sap/ui/core/ComponentContainer"
], (Core, Shell, ComponentContainer) => {
    new Shell("", {
        app: new ComponentContainer("", {
            height: "100%",
            name: "webapp"
        }),
        appWidthLimited: false
    }).placeAt("content");
});

In case it isn't just a matter of a documentation author's taste, I see it important to clarify it in the documentation.

Thanks.

All 5 comments

Hello @pubmikeb !

You should always prefer the function scope for "use strict"; else it would apply to all code which might lead to trouble. See "Strict mode for an entire script is invoked by including the statement "use strict"; before any other statements." Note: "entire script" is not limited to your controller file!

Best regards,
Thomas

Hello @ThomasChadzelek,

thanks for the clarification. The overall strict mode is that I want to achieve, since the strict mode allows to predict the JS behavior in much more confident way. So, do I understand it correctly, that from the UI5 perspective, there should not be any issue in applying the strict mode globally (the rest of the front-end non UI5-code is already written in a strict mode)?

Hello @pubmikeb !

I cannot really answer this question. I hope someone else steps in. I cannot tell if every 3rd party content that is included in UI5 properly works in strict mode.

Best regards,
Thomas

In OpenUI5, strict mode is ensured by the active eslint rules.
In the scope of SAPUI5, not all code uses strict mode, although we recommend it.

I therefore can only recommend to use "use strict" in function scope only, to avoid the problems that @ThomasChadzelek referred to.

To conclude, in OpenUI5, strict mode is ensured by the active ESLint rules, and therefore, can be used everywhere, while in case of SAPUI5 not all code uses strict mode. Hence, it is better to apply "use strict"; in function scope only, to avoid the potentially possible problems.

@codeworrior, thanks for the elaboration!

Was this page helpful?
0 / 5 - 0 ratings