闭包中的值会发生什么? JavaScript [英] What happens with values in closure? JavaScript

查看:127
本文介绍了闭包中的值会发生什么? JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了



关闭:



我想知道内存中会发生什么。我对C#了解不多,引用类型是为每个引用类型在堆中创建对象的原因。 我的问题是,如果F1()的值已经被垃圾收集,如何存储F2()的值?



发生在内存中 - 有多少对象被创建?(JavaScript在堆上创建对象像C#?)或者JavaScript只是使用stack来存储函数的值。

解决方案

每次调用函数时,新的创建包含局部变量的激活上下文。如果内部函数使用任何这些变量,激活上下文被保存(在闭包中),所以局部变量可以在该函数之外访问。



根据激活上下文(函数调用)创建闭包。也就是

  //每次调用doSomething时都会创建一个闭包
// doSomething中的循环创建一个单个闭包是由所有的divs处理程序(或所有处理程序指向相同的函数
//实例及其闭包)共享
//
function doSomething(divs){
for(var i = 0; i< divs.length; i ++){
div.onclick = function(){
//因为只有一个闭包,i将是相同的
// for all divs
console.log('Clicked div,i is'+ i);
}
}
}

//一个闭包
doSomething([div1,div2,div3]);
//一个闭包
doSomething([div9,div10,div11]);

垃圾收集器永远不会垃圾收集仍然引用它的闭包。除此之外,这是内存泄漏的来源之一。在闭包和DOM元素之间通常有一个循环引用,它使IE从垃圾收集闭包,因为它无法检测到DOM元素和常规JavaScript对象之间的循环引用。


I've seen a really cool explanation of closure. So in my view closure is a way to store data somewhere. Let's see examples of closure and example without closure:

Without closure:

function F1(x,y)
{
  var z=5;
  function F2(){
     console.log('x='+x+'y='+y+'z='+z);
  }
  F2();
}

F1(1,2)

With closure:

function F1(x,y)
{
  var z=5;
  function F2(){
     console.log('x='+x+'y='+y+'z='+z);
  }
  return F2();
}

var p=F1(1,2)
p();

I've understood that in closure when F1() finishes its work, then F1() clears connection to the store where values of F1 are kept. But F2() still keeps the reference to the store of values.

Let's see images:

Without closure:

With closure:

I would like to know what happens in memory. I have little knowledge of C# and that reference types are reason to create objects in heap for each reference type. My question is how is it possible to store values of F2() if values of F1() are already garbage collected?

I mean what happens in memory - how many are objects created?(JavaScript creates objects on the heap like C#?) or maybe JavaScript just uses stack to store values of functions?

解决方案

Every time you call a function, a new activation context containing the local variables is created. If inner functions use any of those variables, the activation context is saved (in a closure) so the local variables can be accessed outside of that function.

There is only one closure created per activation context (function call). That is,

// One closure is created every time you call doSomething
// The loop in doSomething creates a single closure that is shared 
// by all the divs handlers (or all the handlers point to the same function
// instance and its closure)
function doSomething(divs) {
   for (var i =0; i < divs.length; i++) {
     div.onclick = function() {
       // Because there is only one closure, i will be the same
       // for all divs
       console.log('Clicked div, i is'  + i);
     }
   }
}

// One closure here
doSomething([div1,div2,div3]);
// One closure here
doSomething([div9,div10, div11]);

The garbage collector will never garbage collect a closure that still has references to it. As an aside, this was one of the sources for memory leaks. There's often a circular reference between a closure and a DOM element, which kept IE from garbage collecting that closure because it couldn't detect that circular reference between DOM elements and regular JavaScript objects.

这篇关于闭包中的值会发生什么? JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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