区分闭包和局部变量

发布于 2024-08-22 19:37:32 字数 435 浏览 7 评论 0原文

闭包中的局部函数声明了一个与闭包中存在的同名变量。那么,我们如何从本地函数访问闭包的变量呢?

function closure()
{
    var xVar; 
    function func1()
    {
        var xVar;
        // how to distinguish local and closure scopes.
        return xVar;
    }
    return function () { return func1(); };
}

创建私有对象并将私有变量作为该对象的属性可能会有所帮助。但我想知道是否有更好、更简洁的解决方案。作用域链有帮助吗?

我已经编辑使其完全关闭。不管怎样,闭包在这里并不是很重要,它可以被考虑用于内部函数,但是,可能有一个带有闭包的解决方案。

谢谢

A local function in the closure declares a variable with the same name which exists in the closure. So, how could we access closure's variable from the local function?

function closure()
{
    var xVar; 
    function func1()
    {
        var xVar;
        // how to distinguish local and closure scopes.
        return xVar;
    }
    return function () { return func1(); };
}

Creating a private object and making private variables as properties of this object could help. But I am wondering if there is a better and neat solution. Can a scope chain help?

I have edited to make it a complete closure. Anyway, closures are not much concern here, it could be considered for inner functions however, there may be a solution with closures somehow.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放肆 2024-08-29 19:37:32

您无法在 JS 中显式访问作用域链。您的问题是变量阴影的古老问题,但它更令人抓狂,因为在 JS 中,作用域链实际上在运行时那里,只是您无法访问它。

如果您使用令人讨厌的 with 运算符,您可以通过重新调整当前作用域来玩一些技巧,但这(以及 arguments 的调用者/被调用者的东西)实际上只是让您访问对象和函数及其属性。没有办法说“从这里告诉我 xVar 在 n-1 运行时范围中的含义”。

You can't access the scope chain explicitly in JS. Your problem is the age-old one of variable shadowing, but it's that much more maddening because in JS, the scope chain is actually there at runtime, it's just not available for you to access.

You can play some tricks with rejiggering current scope if you use the hated with operator, but that (as well as arguments's caller/callee stuff) really just give you access to objects and functions with their properties. There's no way to say "give me what xVar means in the n-1 runtime scope from right here".

被翻牌 2024-08-29 19:37:32

内部作用域中定义的变量隐藏外部作用域中的变量声明。 “更好、更简洁的解决方案”不是以这种方式重用变量名。

Variables defined in an inner scope hide variable declarations in an outer scope. The "better and neat solution" is not to reuse variable names that way.

喜你已久 2024-08-29 19:37:32

在您的示例中, xVar 变量不是闭包,因为您将其范围重新定义为每个函数。要将该变量用作闭包,请继续在closure()函数中使用var命令声明它,然后不要在func1()函数中使用var函数声明它。相反,只需在 func1() 中立即使用该变量即可。

没有一种简单的方法来测试函数是闭包还是局部变量。您必须执行某种流程控制测试,然后分析分配、发生分配的位置以及未发生分配的位置。然后您必须比较这些结果。您可以用 JavaScript 编写一个工具,对给定的输入执行分析,并编写报告作为输出。

In your example the xVar variable is not a closure, because you redefined its scope to each function. To use that variable as a closure continue to declare it with the var command in the closure() function and then do not declare it with the var function in the func1() function. Instead just use the variable immediately in func1().

There is not an easy way to test if a function is a closure or a local variable. You would have to perform some sort of flow control test and then analyze assignments, where assignments occur, and where assignments do not occur. Then you must compare those results. You could write a tool, in JavaScript, to perform that analysis upon a given input and write you a report as output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文