javascript中自执行函数的目的是什么? [英] What is the purpose of a self executing function in javascript?

查看:137
本文介绍了javascript中自执行函数的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,您要什么时候使用:

In javascript, when would you want to use this:

(function(){
    //Bunch of code...
})();

//Bunch of code...


推荐答案

它关于变量范围。在自执行函数中声明的变量,默认情况下,只能用于自执行函数中的代码。这允许在不考虑如何在JavaScript代码的其他块中命名变量的情况下编写代码。

Its all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of how variables are named in other blocks of javascript code.

这篇关于javascript中自执行函数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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