javascript中的变量做用域以及变量提高

在javascript中, 理解变量的做用域以及变量提高是很是有必要的。这个看起来是否很简单,但其实并非你想的那样,还要一些重要的细节你须要理解。javascript

变量做用域

“一个变量的做用域表示这个变量存在的上下文。它指定了你能够访问哪些变量以及你是否有权限访问某个变量。”html

变量做用域分为局部做用域全局做用域。java

局部变量(处于函数级别的做用域)

不像其余对面对象的编程语言(比方说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文档)。app

每个在函数外部声明或者定义的变量都是一个全局对象,因此这个变量能够在任何地方被使用,例如:编程语言

// 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关键字,那么他自动加入到全局做用域中。ide

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
 
最后一点, 在严格模式下,若是没有先声明变量就给变量赋值将会报错!
相关文章
相关标签/搜索