是承诺关闭吗? [英] Is promise a closure?

查看:107
本文介绍了是承诺关闭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关闭标记维基页中,它显示jQuery本身就是一个大关闭。

In closure tag wiki page, it reads "jQuery itself is one big closure."

但是是否也允许关闭呢?你能解释为什么或为什么不?这是我如何理解闭包:分配一个函数到一个变量,并重用它与不同的环境。 Promise使用 $。ajax(),但我在stackoverflow中找不到promise作为一个闭包。也许因为还有其他的功能,如 $。Deferred() resolve()和<$ c $

But is promise a closure as well? Could you please explain why or why not? This is how I understand closure: assign a function to a variable and reuse it with different environments. Promise does that with $.ajax(), but I could not find anywhere in stackoverflow where promise is introduced as a closure. Maybe because there are other features of promise like $.Deferred(), resolve(), and fail() to expand its functionality beyond a simple function passing?

推荐答案

关闭


这是我理解的闭包:将一个函数分配给一个变量,并在不同的环境中重用它。

This is how I understand closure: assign a function to a variable and reuse it with different environments.

这不是一个严格准确的闭包定义。

That's not a strictly accurate definition of a closure.

是可以访问引用环境的函数。在Javascript中,这意味着由另一个函数返回的函数,并且可以访问原始函数作用域。 还有其他SO问题描述得很好

A closure is a function that has access to a referencing-environment. In Javascript, that means a function that is returned by another function and has access to the original functions scope. there are other SO questions that describe this very well

Closure是可以以各种方式使用的通用结构。他们最大的好处之一是它们保护了私有作用域,这就是为什么jQuery类库经常被写为闭包,因此他们不需要在全局范围内公开所有的函数。

Closure's are general purpose structures that can be used in a variety of ways. One of their biggest benefits is that they protect private scope, which is why libraries like jQuery are often written as closures, so that they don't need to expose all their functions globally.

承诺是一个不同的概念。它们是一种结构化异步代码的方式,以便更容易遵循流程。特别地,promise对象是以清楚和容易阅读的方式提供链操作的功能的对象。 promise可以使用闭包来实现,但它不一定是。例如,下面是一个不使用closures的实现:

Promises are a different concept. They are a way of structuring asynchronous code to make it easier to follow the flow. A promise object in particular is an object that provides functions to chain operations in a clear and easy to read way. A promise might be implemented using closures, but it does not have to be. For instance here is an implementation that does not use closures:

https:/ /gist.github.com/814052/690a6b41dc8445479676b347f1ed49f4fd0b1637

而jQuery的实现使用至少一个闭包,但实际上并不是基于它们

whereas jQuery's implementation uses at least one closure, but isn't really based on them

http://james.padolsey。 com / jquery /#v = 1.10.2& fn = jQuery.Deferred

承诺和关闭不是直接相关的概念。 Closure是一种可以在Promise实现中使用的编程技术。最后,实现它不是不可能或不必要的。

Promises and Closures aren't directly related concepts. Closure's are a programming technique that might be used in a Promise implementation. In the end it is neither impossible or necessary to implement it like that.

这篇关于是承诺关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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