本文介绍了JavaScript闭包示例和说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codeproject上找到了这个关闭示例,但是并没有说明它的工作原理。

I have found this example of a closure on codeproject but it does not explain how it works.

    function getMultiplier(multiplyBy){
       function multiply(num){
          return multiplyBy * num;
       }
 
       return multiply;
    }
 
    var multiplyByTwo = getMultiplier(2);
    var multiplyByTen = getMultiplier(10);
    var twoIntoFive = multiplyByTwo(5); 
    var tenIntoSix = multiplyByTen(6);  

    console.log(twoIntoFive); // 10
    console.log(tenIntoSix); // 60

现在我要假设我的C大脑发生了什么事。请纠正我或给出您的解释。

Now i am going to assume, with my C brain what is happening. Please correct me or give your explanation.


  1. JavaScript中的函数也可以是对象,因此内部函数multiple(num)是一种方法

  2. var乘ByTwo,在使用参数2调用时被分配了函数getMultiplier的返回值。

  3. 调用getMultiplier(2 ),则javascript解释器会根据getMultiplier()的定义在内存中创建一个对象。

  4. 该对象具有方法乘法,并且其地址已分配给变量multipliByTwo。

  5. var twoIntoFive = multipleByTwo(5);调用带有参数5的getMultiplier(2)对象的方法遍历(num)。

  6. 将简单数字10返回到变量twoIntoFive

  7. multiplyByTwo = 0;将使javascript的垃圾回收器从内存中删除对象getMultiplier(2)。

  1. functions in javascript can also be objects, so the inner function multiply(num) is a method of getMultiplier, looking from outside.
  2. var multiplyByTwo is assigned the return value of function getMultiplier when invoked with argument 2.
  3. when you call getMultiplier(2), javascript interpreter creates an object in memory based on the definition of getMultiplier().
  4. that object has a method multiply and it's address is assigned to variable multiplyByTwo.
  5. var twoIntoFive = multiplyByTwo(5); calls the getMultiplier(2) object's method multiply(num) with argument 5.
  6. that returns simple numeric 10 into variable twoIntoFive
  7. multiplyByTwo = 0; will make the garbage collector of javascript delete object getMultiplier(2) from memory.


推荐答案

有趣问题。


  1. 实际上,除了基本级别以外,几乎所有正确的JavaScript都是。另外,查看后两个变量 twoIntoFive tenIntoSix multiply() getMultiplier()范围内的独立函数(意味着它也包含对该函数内部变量的引用)。

  1. Almost correct, except that on a basic level, in javascript in fact all functions are objects. Also, looking at the latter two variables, twoIntoFive and tenIntoSix, multiply() is a standalone Function in the scope of getMultiplier() (meaning it also holds a reference to variables inside that function).

完全

正确,但请记住,它仍然不同于 getMultiplier

Correct, but keep in mind that it still differs from getMultiplier.

如,而不是对象,javascript创建了一个接口,该接口引用了函数内部已声明的变量。

As explained in this article, rather than a object, javascript creates an interface with a reference to the already declared variables inside the function.

这就是它的工作方式。但是 twoIntoFive 实际上会调用返回的接口,该接口引用 getMultiplier()函数参数

This is how it seems to work. But twoIntoFive actually calls the returned interface that references the getMultiplier() function parameter

正确。

这是正确的。由于您删除了对该接口的引用,因此该引用也将被删除。

This is correct. Since you removed the reference to the interface, it will be deleted.

进一步阅读

在对javascript有疑问时,通常最好参考MDN。这是我到目前为止阅读的最完整的文档。

It's generally a good idea to refer to MDN when having questions about javascript. It's the most complete documentation I have read so far.





  • Closures on MDN
  • Stackoverflow question linked by commenter @Yordan Nikolov
  • Memory Management (Garbage Collection on MDN)

如果您发现我的答案有错误或有其他疑问,请随时纠正我或提供一些反馈

这篇关于JavaScript闭包示例和说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 09:29