Variable name as a string in Javascript

2018-12-31 22:44发布

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

-- added

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?

标签: javascript
12条回答
其实,你不懂
2楼-- · 2018-12-31 23:11
var somefancyvariable = "fancy";
keys({somefancyvariable})[0];

this isn't able to be made into a function as it returns the name of the function's variable

//THIS DOESN'T WORK
function getVarName(v) {
    return keys({v})[0];
}
//returns "v"
查看更多
妖精总统
3楼-- · 2018-12-31 23:11

Since ECMAScript 5.1 you can use Object.keys to get the names of all properties from an object.

Here is an example:

// Get John’s properties (firstName, lastName)
var john = {firstName: 'John', lastName: 'Doe'};
var properties = Object.keys(john);

// Show John’s properties
var message = 'John’s properties are: ' + properties.join(', ');
document.write(message);

查看更多
倾城一夜雪
4楼-- · 2018-12-31 23:13

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]);

查看更多
ら面具成の殇う
5楼-- · 2018-12-31 23:14

You can use the following solution to solve your problem:

const myFirstName = 'John'
Object.keys({myFirstName})[0]

// returns "myFirstName"
查看更多
人气声优
6楼-- · 2018-12-31 23:16

I needed this, don't want to use objects, and came up with the following solution, turning the question around.

Instead of converting the variable name into a string, I convert a string into a variable.

This only works if the variable name is known of course.

Take this:

var height = 120;
testAlert(height);

This should display:

height: 120

This can be done like this:

function testAlert(ta)
{
    a = window[ta];
    alert(ta + ': ' + a); 
}

var height = 120;
testAlert("height");
// displays: height: 120

So I use the string "height" and turn that into a variable height using the window[] command.

查看更多
只若初见
7楼-- · 2018-12-31 23:16

When having a function write a function that changes different global variables values it is not always myfirstname it is whatever happens to be passing through. Try this worked for me.

Run in jsfiddle

var jack = 'jill';
function window_getVarName(what)
{
  for (var name in window)
  {
    if (window[name]==what)
    return(name);
  }
  return("");
}
document.write(window_getVarName(jack));

Will write to the window 'jack'.

查看更多
登录 后发表回答