Not the value of a variable, but its name.
4 answers
Hmm, yes nothing.
You can, however, make such a trick:
(function() { var getVarName = function tmp(){ let n = /getVarName\(([^)]+?)\)/.exec(tmp.caller !== null ? tmp.caller.toString() : ''); return n !== null ? n[1] : false; } let myVarName123456 = 1; console.info(getVarName(myVarName123456)); // myVarName123456 }()); But this is a dirty hack, uncomfortable, and more than just a βmake to disputeβ than a worker; in real code, I don't even know who could use it.
Perhaps you should reconsider the architecture of your project, where the variable identifier is used as a dependent variable.
Variables just come up with not to depend on the names.
To get the name, you must wrap the variable with an object, like this:
<script> var myVariable = {}; myVariable["myVariable"] = 5; // Π·Π°Π²Π°ΡΠ°ΡΠΈΠ²Π°Π΅ΠΌ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ Π² ΠΌΠ°ΡΡΠΈΠ² myVariable["myVariable"] = 7; // Π²ΠΎΡ ΡΠ°ΠΊ ΡΠ°Π±ΠΎΡΠ°Π΅ΠΌ Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ alert(myVariable["myVariable"]); // Π²ΠΎΡ ΡΠ°ΠΊ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ Π΅Π΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ function getName(arrayVariable){ // ΡΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ ΠΈΠ·Π²Π»Π΅ΡΠ΅Π½ΠΈΡ ΠΈΠΌΠ΅Π½ΠΈ for (key in arrayVariable) return key; } alert(getName(myVariable)); // ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅ΠΌ ΠΈΠΌΡ </script> - oh, what a bad example :-) why is there an array, if it is used as an object? :) - Grundy
- @Grundy in this context, if you wrap a variable in an object, then nothing will change, as it seems to me. - perfect
- that is what it is about, and since it is used only as an object, it is necessary to create an object, not an array - Grundy
- @Grundy Well, if you consider this a matter of principle, then I will correct the code, otherwise someone has already succumbed to your trend and put a minus) - perfect
- Yeah :-) I put it :) well, the
{}literal{}better to use in this case - Grundy
The variable does not store such information about itself, which means you will not be able to get its string representation of the name.
The maximum that you can do is to know the context of a variable and try to get its name through the parent. And that, if only this variable is reference object, instead of a value variable.
function context(){ this.a = { "test" : "test" }; for(key in this){ if(this[key] == a) console.log(key); } } context(); This can be done, but the variable must be an object, and it must be given a unique property or method by which it can be distinguished in the list of other variables. You can get and sort through the list of variables among which the required one is found, inside the document you can
for (var variable in window) { //ΠΠ½Π°Π»ΠΈΠ· variable - ΠΎΠ½Π° ΠΈΠ»ΠΈ Π½Π΅ ΠΎΠ½Π° } Inside you need to use eval and typeof to access the object by its name and determine the presence of a unique property / method. It loads the clientβs browser a bit, but it works. Tested in Fox.
Here is an example.
var Class = function() { } Class.prototype.isOurVariable = function() {return true}; var object123 = new Class(); //ΠΡΠ΅ΠΌ ΠΈΠΌΡ ΡΡΠΎΠΉ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ for (var variable in window) { if (!/[az]+/.test(variable)) { //ΠΎΡΡΠ΅ΠΊΠ°Π΅ΠΌ ΡΠ²ΠΎΠΉΡΡΠ²Π° ΡΠΈΠΏΠ° "0", Π΅ΡΡΡ ΡΠ°ΠΊΠΈΠ΅ continue; } eval("var ok = (" + variable + " != null && typeof (" + variable + ".isOurVariable) != 'undefined');"); if (ok) {console.log(variable); break} } - and if the variable is not in the window? - Grundy Nov.
- and the string eval, is quietly replaced by:
var ok = (window[variable] != null && typeof (window[variable].isOurVariable) != 'undefined');- Grundy Nov. - I have everything in the window. - nikolay yudin
- but i haven't. Any local variable in the function is not added to the window. Therefore, it is worth either adding these restrictions in response, or deleting it - Grundy
- Even simply replacing
var object123withlet object123- the code breaks and does not work - Grundy
var obj = { myFirstName: 'John' }; obj.foo = 'Another name'; for(key in obj) alert(key + ': ' + obj[key]);var obj = { myFirstName: 'John' }; obj.foo = 'Another name'; for(key in obj) alert(key + ': ' + obj[key]);- Alexey Shimansky