Thursday, July 11, 2019

Variable name as a string in Javascript




Is there a way to get a variable name as a string in Javascript? (like NSStringFromSelector in Cocoa)



I would like to do like this:



var myFirstName = 'John';
alert(variablesName(myFirstName) + ":" + myFirstName);

--> myFirstName:John






UPDATE



I'm trying to connect a browser and another program using JavaScript. I would like to send instance names from a browser to another program for callback method:



FooClass = function(){};
FooClass.someMethod = function(json) {
// Do something
}


instanceA = new FooClass();
instanceB = new FooClass();
doSomethingInAnotherProcess(instanceB); // result will be substituted by using instanceB.someMethod();

...


From another program:




evaluateJavascriptInBrowser("(instanceName).someMethod("resultA");");


In PHP:
How to get a variable name as a string in PHP?


Answer



Typically, you would use a hash table for a situation where you want to map a name to some value, and be able to retrieve both.






var obj = { myFirstName: 'John' };
obj.foo = 'Another name';
for(key in obj)
console.log(key + ': ' + obj[key]);




No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...