获取变量名。 JavaScript的"反射" [英] Get variable name. javascript "reflection"

查看:389
本文介绍了获取变量名。 JavaScript的"反射"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让变量名称,就像你可以在净做反思?
像这样的场景:

Is there a way to get the variable name, like you can do in .Net with reflection?
like in this scenario:

function(x,y,z)
{
    if (x === 0)
        logger.log('variable ' + x.reflectedName ' has invalid value ' + x)
        // logs: 'variable x has invalid value 0)
    ...
}

我发现,想变种的名称的功能(?!)之外,但找不到这个问题类似的问题。

I found similar questions that wanted the name of the var outside of the function(?!) but couldn't find this question.

(jQuery是一种选择,但我怎么也想不到它可以用它做...)

(jQuery is an option, though I can't think how it can be done with it...)

推荐答案

.NET 作为一个例子,让我们简单地钻研这一点。在 C#,您可以创建一个函数,它接受一个防爆pression

Since you're using .NET as an example, let's delve briefly into that. In C#, you could create a function that takes an Expression:

void BadArgument<T>(Expression<Func<T>> argExpr)
{
}

但是,为了能够通过调用这个函数提取的变量名,你就必须确保电话始终使用完全正确的语法(即使是没有办法执行这个在编译时):

But in order to be able to extract the variable name from a call to this function, you would have to make sure the call always uses exactly the right syntax (even though there is no way to enforce this at compile time):

if(x < 0)
    BadArgument(() => x);

所以是可以做到的,但它是很脆弱的,pretty的慢。你基本上生成指令的基础上拉姆达EX pression创造一个整体的前pression树()=&GT; X ,只是让你可以调用分析并排除EX pression树,试图找到参数的名称的功能。

So it can be done, but it's very fragile and pretty slow. You're basically generating instructions to create a whole expression tree based on the lambda expression () => x, just so the function you call can parse out that expression tree and try to find the name of the argument.

可这样的事情在JavaScript中做了什么?当然!

在JavaScript中,闭包是通过内部功能产生的,因此,上述的λEX pression相当于是:

In javascript, closures are produced via internal functions, so the equivalent of the above lambda expression would be:

function(){return x;}

此外,由于JavaScript是一种脚本语言,每一个功能是它自己定义为一个字符串的等价物。换句话说,调用的ToString()上述功能将产生:

function(){return x;}

此的jsfiddle显示如何在一个记录式的功能,充分利用这一点。然后,您可以自由地解析产生的函数的字符串,这将是比解析.NET防爆pression树将只有稍微麻烦。此外,获得实际的的的 X 比.NET更简单:你刚才的调用函数

This jsfiddle shows how you can leverage this in a logging-style function. You are then free to parse the resulting function string, which will be only slightly more trouble than parsing the .NET Expression Tree would be. Furthermore, getting the actual value of x is even easier than in .NET: you just call the function!

但是,仅仅因为你的可以的根本,这并不意味着你的应该这是很好的一个哎呀,飕飕的客厅把戏,但是当谈到一直到它,这是不值得的:

But just because you can do it doesn't mean you should. It's nice as a gee-whiz parlor trick, but when it comes right down to it, it's not worth it:

  • 这是脆弱的:如果一些开发商不使用它的权利,并为您提供了一个功能,你不能解析
  • 在它不与缩小工作:想象得到一个消息,变量 A 有一个不正确的值,因为你的缩小的功能,改变了你的变量名
  • 这增加了开销:连minifier不能缩短函数(){返回X;} X
  • 最后,它是复杂的。 这份厚礼说。
  • It's fragile: what if some developer doesn't use it right and gives you a function that you can't parse?
  • It doesn't work with minification: imagine getting a message that variable a had an incorrect value because your minified function changed your variable names.
  • It adds overhead: even a minifier can't shorten function(){return x;} to be smaller than "x".
  • Finally, it's complicated. 'nuff said.

这篇关于获取变量名。 JavaScript的&QUOT;反射&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆