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

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

问题描述

有没有办法获取变量 name,就像您在 .Net 中使用反射所做的那样?
就像在这种情况下:

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)
    ...
}

我发现了类似的问题,需要函数外部的 var 名称(?!),但找不到这个问题.

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# 中,您可以创建一个接受 Expression 的函数:

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

所以它可以做到,但它非常脆弱且非常缓慢.您基本上是在生成指令以根据 lambda 表达式 () => 创建整个表达式树.x,这样你调用的函数就可以解析出表达式树并尝试找到参数的名称.

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中,闭包是通过内部函数产生的,所以上面的lambda表达式的等价物是:

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

function(){return x;}

而且由于 javascript 是一种脚本语言,每个函数都相当于它自己作为字符串的定义.换句话说,对上述函数调用 .toString() 将产生:

And since javascript is a scripting language, each function is the equivalent of its own definition as a string. In other words, calling .toString() on the above function will yield:

function(){return x;}

这个 jsfiddle 展示如何在日志风格的函数中利用它.然后您可以自由地解析生成的函数字符串,这只会比解析 .NET 表达式树稍微麻烦一些.此外,获得 x 的实际 value 比在 .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 的值不正确,因为您的缩小函数更改了您的变量名称.
  • 它增加了开销:即使是压缩器也不能将 function(){return x;} 缩短为小于 "x".
  • 最后,这很复杂.'nuff 说.
  • 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“反射"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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