麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

javascript中的變量作用域以及變量提升詳細介紹

2019-11-20 21:51:00
字體:
來源:轉載
供稿:網友

變量作用域
“一個變量的作用域表示這個變量存在的上下文。它指定了你可以訪問哪些變量以及你是否有權限訪問某個變量。”

變量作用域分為局部作用域和全局作用域。

局部變量(處于函數級別的作用域)
不像其他對面對象的編程語言(比方說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

最后一點, 在嚴格模式下,如果沒有先聲明變量就給變量賦值將會報錯!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美18—19sex性hd | 欧美成人精品h版在线观看 国产一级淫片在线观看 | 欧美a级大胆视频 | 久久国产精品影视 | 久久精品视频1 | 亚洲精中文字幕二区三区 | 特级黄色一级毛片 | 日日草天天干 | 国产一级免费电影 | 久久亚洲精品久久国产一区二区 | 日韩视频区 | 成人男女啪啪免费观看网站四虎 | 狠狠干夜夜草 | 神马久久精品综合 | 天天躁狠狠躁夜躁2020挡不住 | 一级做a爱片性色毛片 | 在线观看视频亚洲 | 亚洲精品动漫在线观看 | 成人啪啪18免费网站 | 国产免费久久久 | av免费在线观看av | 91av在线影院 | 色播视频网站 | 一级一级一级毛片 | 日韩午夜片 | 免费观看黄色片视频 | 亚洲精品一区国产精品丝瓜 | 日本黄色不卡视频 | 狠狠干五月天 | 中国毛片在线观看 | 一区二区三区手机在线观看 | 国产精品久久久久久久久久久天堂 | 国产午夜精品理论片a级探花 | 青青草华人在线 | 久久精品久久久久 | 精品一区二区三区毛片 | 国产在线看一区 | 国产理论视频在线观看 | 在线成人免费观看www | 福利一区二区三区视频在线观看 | av免费片|