为什么使用自动执行功能? [英] Why using self executing function?

查看:77
本文介绍了为什么使用自动执行功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在很多地方我看到这样的脚本:

 (function(){
var hello ='Hello World';
alert(hello);
})();

为什么不把它写成这样,没有任何函数:

  var hello ='Hello World'; 
alert(hello);


解决方案

我们使用自我执行功能来管理变量范围



变量的范围是您的程序的定义区域。
全局变量具有全局范围;它在您的JavaScript代码中随处定义。 (即使在您的功能中)。
另一方面,函数中声明的变量仅在函数体中定义。它们是局部变量并具有局部范围。函数参数也被视为局部变量,并且仅在函数体内定义。

  var scope =global; 
函数checkscope(){
alert(scope);
}

checkscope(); // global

正如您所见,您可以访问范围函数中的变量
,但在函数体内,局部变量优先于具有相同名称的全局变量。如果您声明一个局部变量或函数参数与全局变量名称相同,则可以有效地隐藏全局变量。

  var scope =global; 
函数checkscope(){
var scope =local;
警报(范围);
}

checkscope(); // local
alert(scope); //全局

正如您所看到的,函数内部的变量不会覆盖全局变量。
由于这个特性,我们把代码放在自执行函数
中,以防止覆盖其他变量,当我们的代码变得大而大时

。 b
$ b

  //数千行代码
//一年前写入

//现在您想添加一些代码
//并且你不知道你在过去做了什么
//只需将新代码放入自执行函数
//并且不用担心关于你的变量名

(function(){
var i ='I';
var can ='CAN';
var define ='DEFINE';
var variables ='VARIABLES';
var without ='WITHOUT';
var worries ='WORRIES';

var statement = [i,can,define ,变量,无,后顾之忧];

alert(statement.join(''));
//我可以不需要定义变量
}());




In many places I see scripts like this:

(function () {
    var hello = 'Hello World';
    alert(hello);
})();

Why not just write it like this, without any function:

var hello = 'Hello World';
alert(hello);

解决方案

We use self executing function, to manage Variable Scope.

The scope of a variable is the region of your program in which it is defined. A global variable has global scope; it is defined everywhere in your JavaScript code. (Even in your functions). On the other hand, variables declared within a function are defined only within the body of the function. They are local variables and have local scope. Function parameters also count as local variables and are defined only within the body of the function.

var scope = "global";
function checkscope() {
    alert(scope);
}

checkscope(); // global

As you see, you can access the scope variable inside your function, but, within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable.

var scope = "global";
function checkscope() {
    var scope = "local";
    alert(scope);
}

checkscope(); // local
alert(scope); // global

As you see, variable inside the function, won't overwrite global variables. Because of this feature, we put the code inside the self executing function, to prevent overwriting the other variables, when our code get big and big.

// thousand line of codes
// written a year ago

// now you want to add some peice of code
// and you don't know what you have done in the past
// just put the new code in the self executing function
// and don't worry about your variable names

(function () {
    var i = 'I';
    var can = 'CAN';
    var define = 'DEFINE';
    var variables = 'VARIABLES';
    var without = 'WITHOUT';
    var worries = 'WORRIES';

    var statement = [i, can, define, variables, without, worries];

    alert(statement.join(' '));
    // I CAN DEFINE VARIABLES WITHOUT WORRIES
}());

这篇关于为什么使用自动执行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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