Get the name of the variable. javascript reflection

Is there a way to get the name variable, how can you do in .Net with reflection?
as in this case:

function(x,y,z) { if (x === 0) logger.log('variable ' + x.reflectedName ' has invalid value ' + x) // logs: 'variable x has invalid value 0) ... } 

I found similar questions that wanted the var name outside the function (!), But could not find this question.

(jQuery is an option, although I cannot imagine how this can be done with it ...)

+6
source share
4 answers

Since you are using .NET , briefly talk about it. In C #, you can create a function that accepts Expression :

 void BadArgument<T>(Expression<Func<T>> argExpr) { } 

But in order to be able to extract the variable name from the call to this function, you need to make sure that the call always uses the correct syntax (although this is not possible during compilation);

 if(x < 0) BadArgument(() => x); 

So it can be done, but very fragile and rather slow. You basically generate instructions to create an entire expression tree based on the lambda () => x expression, so the function you call can parse that expression tree and try to find the name of the argument.

Can this be done in javascript? Of course!

In javascript, closures are done through internal functions, so the equivalent of the above lambda expression would be:

 function(){return x;} 

And since javascript is a scripting language, each function is equivalent to its own definition as a string. In other words, calling .toString() for the specified function will give:

 function(){return x;} 

This jsfiddle shows how you can use this in a logging style function. Then you can parse the result string of the function, which will only have a bit more problems than parsing the .NET expression tree. Moreover, getting the actual value of x even easier than in .NET: you just call the function !

But just because you can do it does not mean that you should. . It's nice, like a gee-whiz-style trick, but when it comes to it, it's not worth it:

  • This is fragile: what if some developer does not use it correctly and gives you a function that you cannot analyze?
  • This does not work with minimization: imagine that you received a message that the variable a had the wrong value, because your minified function changed your variable names.
  • It adds overhead: even a minifier cannot reduce function(){return x;} less than "x" .
  • Finally, it is difficult. - he said.
+7
source

You are actually CAN . Here is a snippet:

 function getVarName(v) { for (var key in window) { if (window[key] === v) return key; } } var testvar = 13142; function test(t) { var varName = getVarName(t); // window[varName] -- your variable actualy alert(varName); } test(testvar); // testvar 

Another problem is that you are creating multiple vars that contain the same variable. Then the first var will be returned.

+7
source

You can not. But since you already know the name of the variable (since you must use it to concatenate to the end of the line), why not just enter it?

namely:.

 logger.log('variable x has invalid value '+x); 
+6
source

Easy to get variable name

 //This is variable you want to get the name var prop; var obj = {prop}; for(var propName in obj){ alert(propName); break; } 

also you can try here .

edited

Thanks for the comment @Brady

Object.keys ({variableName}) [0] will give you the name of the variable.

 function(x,y) { const key = (obj) => Object.keys(obj)[0]; if (x === 0) logger.log('variable ' + key({x}) + ' has invalid value ' + x) // logs: 'variable x has invalid value 0) if (y === 0) logger.log('variable ' + key({y}) + ' has invalid value ' + y) // logs: 'variable y has invalid value 0) } 
0
source

Source: https://habr.com/ru/post/911248/


All Articles