javascriptsapui5sap-web-ideui5-toolingblanket.js

const and let declarations in UI5


I would like to know if it's secure to use let and const keywords while working with UI5. I don't know if returning a let, const, or even creating for instance a sap.m.Table with let would have adverse effects.

Maybe someone has some previous experience with this?


Solution

  • I would assume that you are afraid of browser incompatibilities? let and const are constructs for declaring variables; they do not affect object lifetimes. let declares a block scope variable whilst const declares a read-only variable. The objects contained in these variables have a completely different life cycle.

    This is not really a UI5 issue, but a general JavaScript issue. Regardless of the libraries that you are using, the compatibility of your app will be determined by two factors:

    As you can't really control the compatibility matrix of UI5 (which should cover the compatibility matrix for ES6), it all boils down to two major questions that you have to ask yourself:

    For the first question, if you only care for browsers which can support natively ES6 (not IE), then you can definitely use it. Otherwise, the second question might make more sense. If you have a decent C.I. pipeline in place for your app, then you could use something like babel to transform your shiny ES6 code into the verbose spaghetti that IE loves so much.


    Later edit:

    As per the comments, I would point out that ES6 is not a supported in an all-or-nothing fashion. Feature sub-sets (like support for const and let) may be available for some browsers (IE) even if ES6 in its entirety is not. Concretely, based on https://caniuse.com/#feat=let and https://caniuse.com/#feat=const, it seems that let and const are available in IE11 (but not in older versions). Other features like ES6 class definitions are not supported.