typescript 获取打字稿中变量的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29191451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:58:29  来源:igfitidea点击:

Get name of variable in typescript

typescript

提问by Kenneth Bo Christensen

How can I get a variables name in typescript? I want something like this:

如何在打字稿中获取变量名称?我想要这样的东西:

var name = "Foo";
alert(getVariableName(name)); //Prints "name"

回答by Titian Cernicova-Dragomir

Expanding on basarat's answer, you need to create function that takes as a parameter a function that will contain the access to your variable. Because in JavaScript you can access the code of any function it then becomes a simple matter of using a regex to extract the variable name.

扩展 basarat 的答案,您需要创建一个函数,该函数将一个函数作为参数,该函数将包含对您的变量的访问。因为在 JavaScript 中您可以访问任何函数的代码,所以使用正则表达式来提取变量名称变得很简单。

var varExtractor = new RegExp("return (.*);");
export function getVariableName<TResult>(name: () => TResult) {
    var m = varExtractor.exec(name + "");
    if (m == null) throw new Error("The function does not contain a statement matching 'return variableName;'");
    return m[1];
}

var foo = "";
console.log(getVariableName(() => foo));

回答by basarat

TypeScript is JavaScript at runtime. So the same limitations as there apply : Get the 'name' of a variable in Javascript

TypeScript 是运行时的 JavaScript。因此,同样的限制适用:在 Javascript 中获取变量的“名称”

However you can do stuff like

但是你可以做类似的事情

alert(getVariableName(()=>name)) 

Here you would parse the body of the function passed into getVariableName and get that as a string.

在这里,您将解析传递给 getVariableName 的函数体并将其作为字符串获取。

回答by Daniel Earwicker

One approach is to store such values in an object:

一种方法是将这些值存储在一个对象中:

var o = {
    firstName: "Homer",
    lastName: "Simpson"
};

We can't get the name of o, but we can get the names (or "keys") of its two properties:

我们无法获取 的名称o,但我们可以获取其两个属性的名称(或“键”):

var k = Object.keys(o);
console.log(k[0]); // prints "firstName"

回答by Alon

Expanding Cernicova-Dragomir's answer:

扩展 Cernicova-Dragomir 的答案:

Expanding on basarat's answer, you need to create function that takes as a parameter a function that will contain the access to your variable. Because in JavaScript you can access the code of any function it then becomes a simple matter of using a regex to extract the variable name.

扩展 basarat 的答案,您需要创建一个函数,该函数将一个函数作为参数,该函数将包含对您的变量的访问。因为在 JavaScript 中您可以访问任何函数的代码,所以使用正则表达式来提取变量名称变得很简单。

to also support a field of an object:

还支持对象的字段:

var varExtractor = new RegExp("(.*)");

export function getVariableName<TResult>(getVar: () => TResult) {
    var m = varExtractor.exec(getVar + "");

    if (m == null)
        throw new Error("The function does not contain a statement matching 'return variableName;'");

    var fullMemberName = m[1];
    var memberParts = fullMemberName.split('.');

    return memberParts[memberParts.length-1];
}

var foo = { bar: "" };
var varName = getVariableName(() => foo.bar ); //prints "bar"

Notice that I've deleted the "return" because it doesn't work on Node.js when targeting ES6.

请注意,我删除了“return”,因为它在面向 ES6 时不适用于 Node.js。