是否有一种将全局变量转换为局部变量的简单方法? [英] Is there a simple method of turning a global var into a local var?

查看:137
本文介绍了是否有一种将全局变量转换为局部变量的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个如下所示的函数:

Let's say we have a function that looks like this:

const fn = () => x;

此函数应返回 x 的值其中 x 在全局范围内可用。最初这是 undefined 但如果我们定义 x

This function should return the value of x where x is available in the global scope. Initially this is undefined but if we define x:

const x = 42;

然后我们可以预期 fn 返回 42

Then we can expect fn to return 42.

现在假设我们要渲染 fn 作为一个字符串。在JavaScript中,我们为此目的有 toString 。但是我们还要说我们最终要在新的上下文中执行 fn (即使用 eval )所以任何全局引用它的使用应该在我们调用 toString 之前或期间内化。

Now let's say we wanted to render fn as a string. In JavaScript we have toString for this purpose. However let's also say we wanted to eventually execute fn in a new context (i.e. using eval) and so any global references it uses should be internalized either before or during our call to toString.

我们如何制作 x 一个局部变量,其值反映了我们转换时 x 的全局值 fn 到一个字符串? 假设我们无法知道 x 被命名为 x 那说我们可以假设变量是包含在同一个模块中。

How can we make x a local variable whose value reflects the global value of x at the time we convert fn to a string? Assume we cannot know x is named x. That said we can assume the variables are contained in the same module.

推荐答案

我们无法知道 x 被命名 X 。这是这个难题的核心部分,因此在原始问题中加粗。虽然如果我们有一个更简单的解决方案会很好,但它确实似乎是一个正确的答案来实现某种解析器或AST遍历。

We cannot know x is named x. This is the central piece of this puzzle and is therefore bolded in the original question. While it would be nice if we had a simpler solution, it does seem a proper answer here comes down to implementing some kind of parser or AST traversal.

为什么这是必要的?虽然我们可以假设 x 作为全局存在于模块中(它必须在函数之间共享),但我们不能假设它具有已知名称。那么我们需要一些方法从我们的模块中提取 x (或所有全局变量),然后在最终 eval

Why is this necessary? While we can make the assumption that x lives in a module as a global (it's necessarily shared between functions), we cannot assume it has a known name. So then we need some way of extracting x (or all globals really) from our module and then providing it as context when we eventually eval.

注意:提供已知变量作为上下文是微不足道的。这里的几个答案似乎认为这是一个难题,但事实上它很容易用 eval ;只需将上下文作为字符串添加前缀。

N.B.: providing known variables as context is trivial. Several answers here seem to assume that's a difficult problem but in fact it's quite easy to do with eval; simply prepend the context as a string.

那么这里的正确答案是什么?如果我们使用AST(例如, Acorn 可能是一个可行的起点)我们可以检查模块并以编程方式提取其中的所有全局变量。这包括 x 或我们的函数之间可能共享的任何其他变量;我们甚至可以检查这些函数来确定哪些变量是执行它们所必需的。

So then what's the correct answer here? If we were to use an AST (Acorn may be a viable starting point, for instance) we could examine the module and programmatically extract all the globals therein. This includes x or any other variable that might be shared between our functions; we can even inspect the functions to determine which variables are necessary for their execution.

再次提出这个问题的希望再次是提炼一个更简单的解决方案或揭示现有技术可能会适应我们的需要。最终我的答案和我接受的答案归结为从JavaScript模块解析和提取全局变量的重要任务;似乎没有一种简单的方法。我认为如果不是我们今天实施的实际问题,这是一个公平的答案。 (然而,随着项目的增长,我们将在稍后解决这个问题。)

Again the hope in asking this question originally was to distill a simpler solution or uncover prior art that might be adapted to fit our needs. Ultimately my answer and the answer I'm accepting comes down to the nontrivial task of parsing and extracting globals from a JavaScript module; there doesn't appear to be a simple way. I think this is a fair answer if not a practical one for us to implement today. (We will however address this later as our project grows.)

这篇关于是否有一种将全局变量转换为局部变量的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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