JavaScript closure regarding unregistered variables

I know of the great posts about Closures here and here , but it seems to be the specific case that I have in mind. The question is best demonstrated using code:

function foo() {
    var x = {};
    var y = "whatever";

    return function bar() {
        alert(y);
    };
}

var z = foo();

The link yinside barcauses it to close, and as long as I keep zaround the garbage collector it will not clear y. The question is what happens to x? Is this holding this closure even if it is not referenced? Will the garbage collector see the link xand clear it? Or xsaved along with ywhile I hold on z? (An ideal answer would lead to an ECMA specification.)

+4
source share
1 answer

The question is what happens to x?

The answer depends on the theory and implementation.

In theory, yes, it is xkept alive, since a closure (anonymous function) has a link to the call context binding object foothat includes x.

JavaScript . , x , . , , . : V8 ( Chrome ) x, y , x , ; foo , , . , .: -)

, ? , eval new Function, JavaScript, , , x .


, x , GC , () , :

x = undefined;

, x, . , , x , , , . . , , , , , , t, , .


, , , , . , . , ...

V8, Chrome heaphot:

function UsedFlagClass_NoFunction() {}
function UnusedFlagClass_NoFunction() {}
function build_NoFunction() {
  var notused = new UnusedFlagClass_NoFunction();
  var used = new UsedFlagClass_NoFunction();
  return function() { return used; };
}

function UsedFlagClass_FuncDecl() {}
function UnusedFlagClass_FuncDecl() {}
function build_FuncDecl() {
  var notused = new UnusedFlagClass_FuncDecl();
  var used = new UsedFlagClass_FuncDecl();
  function unreachable() { notused; }
  return function() { return used; };
}

function UsedFlagClass_FuncExpr() {}
function UnusedFlagClass_FuncExpr() {}
function build_FuncExpr() {
  var notused = new UnusedFlagClass_FuncExpr();
  var used = new UsedFlagClass_FuncExpr();
  var unreachable = function() { notused; };
  return function() { return used; };
}

window.noFunction = build_NoFunction();
window.funcDecl = build_FuncDecl();
window.funcExpr = build_FuncExpr();

:

no description available

build_NoFunction V8 , , notused, , , , unreachable , notused .

, , ?

, , , JavaScript-to-JavaScript, Google Closure Compiler. "" "" Closure Compiler :

function UsedFlagClass_NoFunction() {}
function UnusedFlagClass_NoFunction() {}
function build_NoFunction() {
    new UnusedFlagClass_NoFunction;
    var a = new UsedFlagClass_NoFunction;
    return function () {
        return a
    }
}

function UsedFlagClass_FuncDecl() {}
function UnusedFlagClass_FuncDecl() {}
function build_FuncDecl() {
    new UnusedFlagClass_FuncDecl;
    var a = new UsedFlagClass_FuncDecl;
    return function () {
        return a
    }
}

function UsedFlagClass_FuncExpr() {}
function UnusedFlagClass_FuncExpr() {}
function build_FuncExpr() {
    new UnusedFlagClass_FuncExpr;
    var a = new UsedFlagClass_FuncExpr;
    return function () {
        return a
    }
}
window.noFunction = build_NoFunction();
window.funcDecl = build_FuncDecl();
window.funcExpr = build_FuncExpr();

, CC, unreachable , .

, , , , unreachable - . , , , . :

unused = undefined;

. , :

unused = unreachable = undefined;

(, , .)

, , :

unreachable = undefined;

... ( ) V8, unused .: - (

+7

Source: https://habr.com/ru/post/1546418/


All Articles