我们是否仅因为"JS规则"就不能使用函数中的变量? [英] Do we can't use the variables from function just because of "JS rules"?

查看:50
本文介绍了我们是否仅因为"JS规则"就不能使用函数中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个有点奇怪的问题.但我会解释我的意思.

I think it's a little bit strange question. But I'll explain what I mean.

事实证明,在此示例中,标记扫掠算法在从该函数中删除后清除了对象引用.

It turns out that Mark-and-sweep algorithm clears the object references in this example after going out from this function.

function f() {
    let a = 'some text';
    var obj1 = {};
    var obj2 = {};
    obj1.p = obj2; // obj1 references obj2
    obj2.p = obj1; // obj2 references obj1. This creates a cycle.
}
f();

因此,如果标记扫掠"只能清除对象,则此函数中的所有变量都保留吗?我的意思是我们不能在功能之外使用它们.那是因为"JS规则"吗?

So, if Mark-and-sweep can clear only objects, do all variables in this function stay? I mean that we can't use them out of the function. Is that behaviour just because of "JS rules"?

P.S.我的意思是,在了解这些信息之前,我认为我们不能使用函数中的所有变量,因为在退出该函数后它们将被删除.

P. S. I mean that before knowing this information I thought that we can't use all variables from function because they are deleted after we go out from this function.

推荐答案

事实证明,在此示例中,标记扫掠算法从该函数中删除后会清除对象引用

It turns out that Mark-and-sweep algorithm clears the object references in this example after going out from this function

可以 进行标记和扫描.它可能是引用计数.它们可以在堆栈上(是的,对象可以在堆栈上).它可以是其他几种垃圾回收策略中的任何一种.归根结底,用JavaScript术语来说重要的是,一旦该函数返回,这些对象都不是可访问的,因此JavaScript引擎可以使用任何启发式或算法来删除这些对象从记忆里.(它也可以这样做,垃圾收集不是强制性的,但是在特定环境(例如某些嵌入式环境)之外的现实世界中是普遍存在的.)

It could be mark-and-sweep. It could be reference counting. They could be on the stack (yes, objects can be on the stack). It could be any of several other garbage collection strategies. At the end of the day, all that matters in JavaScript terms is that neither of those objects is reachable anymore once that function returns, and so the JavaScript engine can — using any heuristic or algorithm — remove those objects from memory. (It can also not do so, garbage collection isn't mandatory, but it's universal in the real world outside of specialized environments, such as some embedded ones.)

那么...此函数中的所有变量都保留吗?我的意思是我们不能在功能之外使用它们.那是因为"JS规则"吗?

So...do all variables in this function stay? I mean that we can't use them out of the function. Is that behaviour just because of "JS rules"?

由于没有任何人使用它们,因此它们很可能不会留下.

None of them stay, most likely, since nothing uses any of them.

P.S.我的意思是,在了解这些信息之前,我认为我们不能使用函数中的所有变量,因为在退出该函数后它们将被删除.

P. S. I mean that before knowing this information I thought that we can't use all variables from function because they are deleted after we go out from this function.

这取决于是否在函数内部创建了一旦函数返回就被引用的闭包.在您的示例中,没有,因此可以从内存中删除对 f 的整个调用上下文.但这不是真的:

That depends on whether any closures are created within the function that are referenced once the function returns. In your example, none are, so the entire context of the call to f can be removed from memory. But that's not true here:

function counter(value) {
    return function() {
        return value++;
    };
}

const f = counter(0);
console.log(f()); // 0
console.log(f()); // 1
console.log(f()); // 2

在那里,返回的函数关闭了调用 counter 的环境,因此 value 参数(本质上是(与局部变量相同)未清除,因此保留它是因为保留了环境,因为该函数具有对它的引用.在该代码中,只要 f 常量对函数的引用,环境和其中的 value 都会保留.

There, the function that is returned closes over the environment of the call to counter, so the value parameter (which is essentially the same as a local variable) is not cleaned up, it's retained because the environment is retained because the function has a reference to it. In that code, as long as the f constant has a reference to the function, the environment and the value within it are retained.

更多:

这篇关于我们是否仅因为"JS规则"就不能使用函数中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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