您好,欢迎访问一九零五行业门户网

javascript中的变量作用域以及变量提升详细介绍_javascript技巧

变量作用域
“一个变量的作用域表示这个变量存在的上下文。它指定了你可以访问哪些变量以及你是否有权限访问某个变量。”
变量作用域分为局部作用域和全局作用域。
局部变量(处于函数级别的作用域)
不像其他对面对象的编程语言(比方说c++,java等等),javascript没有块级作用域(被花括号包围的);当是,javascript有拥有函数级别的作用域,也就是说,在一个函数内定义的变量只能在函数内部访问或者这个函数内部的函数访问(闭包除外,这个我们过几天再写个专题)。
函数级别作用域的一个例子:
复制代码 代码如下:
var name = richard;
function showname () {
    var name = jack; // local variable; only accessible in this showname function
    console.log (name); // jack
}
console.log (name); // richard: the global variable
没有块级作用域:
复制代码 代码如下:
var name = richard;
// the blocks in this if statement do not create a local context for the name variable
if (name) {
    name = jack; // this name is the global name variable and it is being changed to jack here
    console.log (name); // jack: still the global variable
}
// here, the name variable is the same global name variable, but it was changed in the if statement
console.log (name); // jack//    不要忘记使用var关键字
//    如果声明一个变量的时候没有使用var关键字,那么这个变量将是一个全局变量!
// if you don't declare your local variables with the var keyword, they are part of the global scope
var name = michael jackson;
function showcelebrityname () {
    console.log (name);
}
function showordinarypersonname () {   
    name = johnny evers;
    console.log (name);
}
showcelebrityname (); // michael jackson
// name is not a local variable, it simply changes the global name variable
showordinarypersonname (); // johnny evers
// the global variable is now johnny evers, not the celebrity name anymore
showcelebrityname (); // johnny evers
// the solution is to declare your local variable with the var keyword
function showordinarypersonname () {   
    var name = johnny evers; // now name is always a local variable and it will not overwrite the global variable
    console.log (name);
}
//    局部变量优先级大于全局变量
//如果在全局作用域中什么的变量在局部作用域中再次声明,那么在局部作用域中调用这个变量时,优先调用局部作用域中声明的变量:
var name = paul;
function users () {
    // here, the name variable is local and it takes precedence over the same name variable in the global scope
var name = jack;
// the search for name starts right here inside the function before it attempts to look outside the function in the global scope
console.log (name);
}
users (); // jack
全局变量
所有在函数外面声明的变量都处于全局作用域中。在浏览器环境中,这个全局作用域就是我们的window对象(或者整个html文档)。
每一个在函数外部声明或者定义的变量都是一个全局对象,所以这个变量可以在任何地方被使用,例如:
复制代码 代码如下:
// name and sex is not in any function
var myname = zhou;
var sex = male;
//他们都处在window对象中
console.log(window.myname); //paul
console.log('sex' in window); //true
如果一个变量第一次初始化/声明的时候没有使用var关键字,那么他自动加入到全局作用域中。
复制代码 代码如下:
function showage(){
  //age初始化时没有使用var关键字,所以它是一个全局变量
  age = 20;
  console.log(age);
}
showage();  //20
console.log(age); //因为age是全局变量,所以这里输出的也是20
settimeout中的函数是在全局作用域中执行的settimeout中的函数所处在于全局作用域中,所以函数中使用this关键字时,这个this关键字指向的是全局对象(window):
复制代码 代码如下:
var value1 = 200;
var value2 = 20;
var myobj = {
  value1 : 10,
  value2 : 1,
caleculatedit: function(){
    settimeout(function(){
      console.log(this.value1 * this.value2);
    }, 1000);
  }
}
myobj.caleculatedit(); //4000
为了避免对全局作用域的污染, 所以一般情况下我们尽可能少的声明全局变量。 
变量提升(variable hoisting)
所以的变量声明都会提升到函数的开头(如果这个变量在这个函数里面)或者全局作用域的开头(如果这个变量是一个全局变量)。我们来看一个例子:
复制代码 代码如下:
function showname () {
console.log (first name: + name);
var name = ford;
console.log (last name: + name);
}
showname ();
// first name: undefined
// last name: ford
// the reason undefined prints first is because the local variable name was hoisted to the top of the function
// which means it is this local variable that get calls the first time.
// this is how the code is actually processed by the javascript engine:
function showname () {
    var name; // name is hoisted (note that is undefined at this point, since the assignment happens below)
console.log (first name: + name); // first name: undefined
name = ford; // name is assigned a value
// now name is ford
console.log (last name: + name); // last name: ford
}
函数声明会覆盖变量声明
如果存在函数声明和变量声明(注意:仅仅是声明,还没有被赋值),而且变量名跟函数名是相同的,那么,它们都会被提示到外部作用域的开头,但是,函数的优先级更高,所以变量的值会被函数覆盖掉。
复制代码 代码如下:
// both the variable and the function are named myname
var myname;?
function myname () {
console.log (rich);
}
// the function declaration overrides the variable name
console.log(typeof myname); // function
但是,如果这个变量或者函数其中是赋值了的,那么另外一个将无法覆盖它:
复制代码 代码如下:
// but in this example, the variable assignment overrides the function declaration
var myname = richard; // this is the variable assignment (initialization) that overrides the function declaration.
function myname () {
console.log (rich);
}
console.log(typeof myname); // string
最后一点, 在严格模式下,如果没有先声明变量就给变量赋值将会报错!
其它类似信息

推荐信息