以下 JavaScript 构造是否称为闭包? [英] Is the following JavaScript construct called a Closure?

查看:18
本文介绍了以下 JavaScript 构造是否称为闭包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(function() {

  //do stuff

})();

我最初认为这个构造被称为闭包——不是它在闭包中导致结果(可能)的效果——如果变量被捕获.

I originally thought this construct was called a closure - not that the effect that it caused results (potentially) in a closure - if variables are captured.

这与闭包本身的行为无关 - 我完全理解这一点,而不是被问到的问题.

This is in no way to do with the behaviour of closures themselves - this I understand fully and was not what was being asked.

推荐答案

它是一个匿名函数(或者更准确地说是一个 作用域匿名函数) 立即执行.

It is an anonymous function (or more accurately a scoped anonymous function) that gets executed immediately.

使用 one 是在其中声明的任何变量和函数都限定在该函数的范围内,因此对任何全局上下文都是隐藏的(因此您可以获得封装和信息隐藏).

The use of one is that any variables and functions that are declared in it are scoped to that function and are therefore hidden from any global context (so you gain encapsulation and information hiding).

这篇关于以下 JavaScript 构造是否称为闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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