How to check that the ES6 variable is constant?

Does anyone know any tricks how to do this? I tried using try-catch :

 "use strict"; const a = 20; var isConst = false; try { var temp = a; a = a+1; a = temp; } catch (e) { isConst = true; } 

But, unfortunately, it only works in "strict" mode. Without "strict use" he executes all statements in silence, without changing a . Also, I cannot wrap this code in some convenient function (for example, isConstant(someConst) ), since any argument that I pass to this function will be a new variable. So, does anyone know how to create the isConstant() function?

+5
source share
3 answers

I do not think there is, but I also do not think that this is a big problem. I think it would be useful to be able to know if there is a const variable, and it exists in some other languages, but in fact, since you (or someone from the team) will define these variables, you would know the scope and type of variables . In other words, you cannot, but that is not a problem either.

The only time this can be useful is to change the mutable property at run time, and if changing this property has actual performance benefits; let , const and var considered roughly the same for the compiler, the only difference is that the compiler monitors the const and checks the assignments before compiling it.

Another thing to note is that, like let , const bound to the current scope, so if you have something like this:

 'use strict'; const a = 12; // another scope { const a = 13; } 

it's really. Just be careful that it looks in higher areas unless you explicitly specify const a = 13 in this new area, and it will give a Read Only or Assignment error:

 'use strict'; const a = 12; { a = 13; // will result in error } 
+1
source

It seems impossible right out of the box. For the time being, it is best to stick with transpilers for ES6 development, and they are consistent with throwing errors specific to ES6 features.

It is useful to adhere to the UPPERCASE_CONSTANT agreement, which is used in other languages, it has been specifically adopted to avoid such cases.

0
source

Just check if your reassignment is valid:

 var isConst = function(name, context) { // does this thing even exist in context? context = context || this; if(typeof context[name] === "undefined") return false; // if it does exist, a reassignment should fail, either // because of a throw, or because reassignment does nothing. try { var _a = context[name]; context[name] = !context[name]; if (context[name] === _a) return true; // make sure to restore after testing! context[name] = _a; } catch(e) { return true; } return false; }.bind(this); 

You need to try / catch, because override Can throws an exception (for example, in Firefox), but when it is not (for example, in Chrome), you just check if something really changed the value of "this always changes the value" .

A simple test:

 const a = 4; var b = "lol"; isConst('a'); // -> true isConst('b'); // -> false 

And if you declare consts in a different context, pass that context to force permission on the correct object.

Disadvantage

: this will not work on vars declared outside of the scope of objects. upside: there is absolutely no point in announcing them elsewhere. For example, declaring them in the scope of functions makes the const keyword mostly useless:

 function add(a) { return ++a; } function test() { const a = 4; console.log(add(a)); } test(); // -> 5 

Although a is constant inside test (), if you pass it to anything else, it is passed as a regular mutable value, because now it is just a β€œthing” in the arguments list.

In addition, the only reason to have const is because it does not change. Thus, constantly recreating it, because you call a function that uses it more than once, means that your const should live outside the function instead, so again we are forced to put the variable in the area of ​​the object.

0
source

All Articles