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

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 .: - (