变量名作为Javascript中的字符串有没有办法在Javascript中以字符串的形式获得变量名?(比如NSStringFromSelector在……里面可可粉)我想这样做:var myFirstName = 'John';alert(variablesName(myFirstName) + ":" + myFirstName);--> myFirstName:John更新我试图用JavaScript连接一个浏览器和另一个程序。我希望将实例名称从浏览器发送到另一个程序,以便回调方法:FooClass = function(){};FooClass.someMethod = function(json) {
// Do something}instanceA = new FooClass();instanceB = new FooClass();doSomethingInAnotherProcess(instanceB);
// result will be substituted by using instanceB.someMethod();...来自另一个项目:evaluateJavascriptInBrowser("(instanceName).someMethod("resultA");");在PHP中:如何在PHP中以字符串的形式获得变量名?
3 回答
慕雪6442864
TA贡献1812条经验 获得超5个赞
就像塞斯的答案,但是用Object.keys()相反:
const varToString = varObj => Object.keys(varObj)[0]
const someVar = 42
const displayName = varToString({ someVar })
console.log(displayName)
添加回答
举报
0/150
提交
取消