Javascript 从typescript方法中获取方法名称
我想从Typescript中类的实例方法中获取当前方法的名称 (伪代码,不起作用): 我希望“某物”返回“酒吧”。我意识到,Javascript 从typescript方法中获取方法名称,javascript,typescript,Javascript,Typescript,我想从Typescript中类的实例方法中获取当前方法的名称 (伪代码,不起作用): 我希望“某物”返回“酒吧”。我意识到,this可以给我类,我可以从中获得类及其属性,但我不知道如何获得“this function”(即方法栏,而不是类Foo) 我还看到了其他几个与查找类名等相关的问题,但没有一个问题涉及到获取当前方法名。除此之外,没有直接的方法获取此名称 我提议另外两种方法: 使用装饰器插入方法名称: function annotateName(target, name, desc) {
this
可以给我类,我可以从中获得类及其属性,但我不知道如何获得“this function”(即方法栏,而不是类Foo)
我还看到了其他几个与查找类名等相关的问题,但没有一个问题涉及到获取当前方法名。除此之外,没有直接的方法获取此名称
我提议另外两种方法:
使用装饰器插入方法名称:
function annotateName(target, name, desc) {
var method = desc.value;
desc.value = function () {
var prevMethod = this.currentMethod;
this.currentMethod = name;
method.apply(this, arguments);
this.currentMethod = prevMethod;
}
}
class Foo {
currentMethod: string;
@annotateName
bar() {
alert(this.currentMethod);
this.tux();
alert(this.currentMethod);
}
@annotateName
tux() {
alert(this.currentMethod);
}
}
new Foo().bar();
缺点是,您必须注释所有想要从中获取名称的函数。您可以只对类进行注释,然后在decorator中迭代所有原型函数并应用相同的思想
我的第二个选择不是标准化的,需要更加小心才能在不同浏览器中获得一致的结果。它依赖于创建错误对象并获取其跟踪
不确定这是否有帮助,但:
class-Foo{
bar(){
console.log(Object.getOwnPropertyNames(Foo.prototype));//[“constructor”,“bar”]
}
}
新Foo().bar()代码>对于类名-Foo.name
对于方法名称-this.bar.name
,您可以做(但不应该做)如下操作,只需用另一个有趣的例子回答问题:
class Foo {
constructor(private http: HttpClient) {
const apiUrl = 'http://myapi.com/api/';
{
const functionName = 'getBar';
this[functionName] = function () {
return http.get(apiUrl + functionName);
}
}
{
const functionName = 'postBar';
this[functionName] = function () {
return http.get(apiUrl + functionName);
}
}
{
const functionName= 'putBar';
this[functionName] = function () {
return http.get(apiUrl + functionName);
}
}
{
const functionName= 'deleteBar';
this[functionName] = function () {
return http.get(apiUrl + functionName);
}
}
}
}
这当然不是一个优雅的解决方案,我真的无法想象这样做的好用例,因为我很确定编译器不会识别new Foo(http).deleteBar()
。也许有人能用这个想法想出一个优雅的解决方案,我将把它作为一个实验留给其他人
但是使用这种模式(如果您使用某种devops脚手架或“强大的复制粘贴技能”),您总是可以通过functionName
访问方法的名称。请记住,在编译和缩小过程中,您可能会丢失您试图使用的实际名称。你可能会考虑在编译过程中查看BabelGe宏,它读取几乎任何东西的名称,并返回它的实际字符串表示。 < P>我也在寻找一个解决方案,试试这个:
class Foo {
bar() {
console.log(this.bar.name); // <-- Print out the function name.
}
}
new Foo().bar();
class-Foo{
bar(){
console.log(this.bar.name);//为什么您需要知道方法的名称?您调用它是为了知道名称。这不是一个真正的typescript问题…:)@Rienk-我正在尝试做一些更复杂的事情(具体地说,ruby的方法在typescript中缺少行为),但作为一个简单的示例,假设我有带有(大量方法),如getFoo()
和getBar()
,我想使用getObject(objName)
函数来处理所有这些,该函数解析函数名,并按名称从字典返回对象。我想调用getObject(thisfunname)
在每个这样的函数中,而不必更改每个这样的函数来表示getObject('foo')之类的内容
,等等。阿南德:然后用你想用TypeScript实现的模式打开一个新问题。我认为这样会更好。正如@Kokodoko所说,这不是一个TypeScript,而是一个JavaScript。有,但它不能在严格模式下工作,类方法是在严格模式下执行的。你可以尝试使用它,但不是全部浏览器支持代理。问题是关于如何获取当前正在执行的方法的名称。请解释在annotateName
函数中从何处获取this
和arguments
。@luukvhoudtthis
和arguments
都是javascript.Chec函数中的特殊变量k out和。但是这个.bar.name
比只键入bar
要长。
class Foo {
constructor(private http: HttpClient) {
const apiUrl = 'http://myapi.com/api/';
{
const functionName = 'getBar';
this[functionName] = function () {
return http.get(apiUrl + functionName);
}
}
{
const functionName = 'postBar';
this[functionName] = function () {
return http.get(apiUrl + functionName);
}
}
{
const functionName= 'putBar';
this[functionName] = function () {
return http.get(apiUrl + functionName);
}
}
{
const functionName= 'deleteBar';
this[functionName] = function () {
return http.get(apiUrl + functionName);
}
}
}
}
class Foo {
bar() {
console.log(this.bar.name); // <-- Print out the function name.
}
}
new Foo().bar();