简体   繁体   English

闭包与回调有什么不同?

[英]How is a closure different from a callback?

I asked a question about callbacks and arrived at another question (see comment). 我问了一个关于回调的问题并得出了另一个问题(见评论)。 How is a closure different from a callback? 闭包与回调有什么不同?

Check the introduction in this: http://jibbering.com/faq/faq_notes/closures.html . 请查看以下内容: http//jibbering.com/faq/faq_notes/closures.html It can help you understand better how closures relate to functions. 它可以帮助您更好地理解闭包与函数的关系。

Here is a set of closure examples: http://www.javascriptkit.com/javatutors/closures2.shtml 这是一组闭包示例: http//www.javascriptkit.com/javatutors/closures2.shtml

Basically, the callback is like a function pointer. 基本上,回调就像一个函数指针。 The bit that makes it a closure, is when that function accesses anything on the context where it lives, like variables outside it. 使它成为闭包的位是当该函数访问它所在的上下文中的任何内容时,就像它之外的变量一样。 When that happens, the function will use the current values of the variables (as opposed to copy them). 当发生这种情况时,该函数将使用变量的当前值(而不是复制它们)。 See example 4. 见例4。

Different definitions: 不同的定义:

Callback - 回调 -

a callback is executable code that is passed as an argument to other code. 回调是可执行代码,作为参数传递给其他代码。

Closure - 关闭 -

a closure is a function that is evaluated in an environment containing one or more bound variables. 闭包是在包含一个或多个绑定变量的环境中计算的函数。 When called, the function can access these variables. 调用时,该函数可以访问这些变量。

简单来说:使用上下文变量的回调是一个闭包。

There's a good definition of closures here : 有倒闭的一个很好的定义, 在这里

A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression). “闭包”是一个表达式(通常是一个函数),它可以将自由变量与绑定这些变量的环境(“关闭”表达式)结合在一起。

In practice, that means it's a function that has some hidden variables. 在实践中,这意味着它是一个具有一些隐藏变量的函数。

A callback is a higher-level idea. 回调是一个更高层次的想法。 Generally it is a function which is passed around with the intent of being called at a later time. 通常,它是一个传递的函数,其目的是在稍后调用。 In JavaScript, closures are often used as callbacks. 在JavaScript中,闭包通常用作回调。

A callback depending on a context variable aka bound variables (== object state) will be a closure. 根据上下文变量(即绑定变量(==对象状态))的回调将是一个闭包。 It will be a pure function, otherwise, when it only takes free variables (== parameters). 它将是一个纯函数,否则,它只需要自由变量(==参数)。

Here is a way to differentiate between those two: 以下是区分这两者的方法:

Closure 关闭

A Closure is used to extend functionality, for instance if a user clicks a button, we want something to happen on the screen, in that case, we would use a Closure where we pass the user event (a click) and then push data to the view. Closure用于扩展功能,例如,如果用户单击按钮,我们希望在屏幕上发生某些事情,在这种情况下,我们将使用Closure来传递用户事件(单击)然后将数据推送到风景。

Callback 打回来

A callback is more or less similar to a closure, but it is more used to inform and provide synchronous capabilities. 回调或多或少类似于闭包,但它更习惯于通知并提供同步功能。 For instance if you perform jQuery Ajax calls, you'll have callbacks such as success() , error() , beforeSend() and so forth to handle the asynchronous data. 例如,如果你执行jQuery Ajax调用,你将有回调,如success()error()beforeSend()等等来处理异步数据。

closure : 关闭:

  • A function keyword inside another function, you are creating a closure 另一个函数中的函数关键字,您正在创建一个闭包

  • Or A function return to an other function we can say closure 或者一个函数返回另一个函数,我们可以说是闭包

Note Plain English : A little bit difference function passing as argument in another function is callback or if define in another function is closure 注意简单英语:在另一个函数中作为参数传递的一点点差异函数是回调,或者如果在另一个函数中定义是闭包

 var length = 101; function fn2() { console.log("fffxxx: "+this.length); } var obj = { length: 5, method3: function(fn) { fn(); arguments[0](); } }; obj.method3(fn2, 1); 
**Outputहोगा **输出होगा

 fffxxx:101 fffxxx:2** 

What is a callback function? 什么是回调函数?

A callback function is a function which is: 回调函数是一个函数,它是:

  • passed as an argument to another function 作为参数传递给另一个函数
  • is invoked(लागू) after some kind of event 在某种事件之后被调用(लागू)
  • once its parent function completes, the function passed as an argument is then called 一旦其父函数完成,则调用作为参数传递的函数

in Plain English we say A callback is any function that is called by another function, which takes the first function as a parameter or function passed as an argument 在简单英语中,我们说 回调是由另一个函数调用的任何函数,它将第一个函数作为参数或作为参数传递的函数

  • Note : invoked : The code inside a function is executed when the function is invoked. 注意: 调用 :函数内部的代码在调用函数时执行。 or we say like this It is common to use the term "call a function" instead of "invoke a function". 或者我们这样说通常使用术语“调用函数”而不是“调用函数”。

It is also common to say "call upon a function", "start a function", or "execute a function". 通常说“调用函数”,“启动函数”或“执行函数”。

  function getUserInput(firstName, lastName, age, callback2,callback1) { var fullName = firstName + " " + lastName; // Make sure the callback is a function if (typeof callback2 === "function") { // Execute the callback function and pass the parameters to it callback2(fullName, age); } if (typeof callback1 === "function") { callback1(lastName); } } function callbackforlastname1(lname){ console.log("---"); } function genericPoemMaker(name, aged) { console.log(name + " is finer than fine wine."); console.log("A " + aged + " of unfortunl smile"); } getUserInput("Avinash", "Maurya", "26", genericPoemMaker,callbackforlastname1); 
ऐसे कॉल करते है ऐसेकॉलकरतेहै

I fail to see how the two are even related? 我看不出这两者是如何相关的? A closure carries parts of a local state into a function of some sort, think of it as passing by reference. 闭包将局部状态的某些部分转化为某种功能,将其视为通过引用传递。

A callback is meant to notify you about certain change and it redirects program flow. 回调旨在通知您某些更改,并重定向程序流。 The closure could modify local state but you would never get processor time to handle that, like you would with a callback. 闭包可以修改本地状态,但是你永远不会得到处理器时间来处理它,就像你使用回调一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM