最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2013年10月24日 16:22:57   作者:  
在javascript中, 理解變量的作用域以及變量提升是非常有必要的。這個看起來是否很簡單,但其實并不是你想的那樣,還要一些重要的細(xì)節(jié)你需要理解

變量作用域
“一個變量的作用域表示這個變量存在的上下文。它指定了你可以訪問哪些變量以及你是否有權(quán)限訪問某個變量?!?/P>

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

局部變量(處于函數(shù)級別的作用域)
不像其他對面對象的編程語言(比方說C++,Java等等),javascript沒有塊級作用域(被花括號包圍的);當(dāng)是,javascript有擁有函數(shù)級別的作用域,也就是說,在一個函數(shù)內(nèi)定義的變量只能在函數(shù)內(nèi)部訪問或者這個函數(shù)內(nèi)部的函數(shù)訪問(閉包除外,這個我們過幾天再寫個專題)。

函數(shù)級別作用域的一個例子:


復(fù)制代碼 代碼如下:

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

沒有塊級作用域:


復(fù)制代碼 代碼如下:

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關(guān)鍵字
//    如果聲明一個變量的時候沒有使用var關(guān)鍵字,那么這個變量將是一個全局變量!
// 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);
}
//    局部變量優(yōu)先級大于全局變量
//如果在全局作用域中什么的變量在局部作用域中再次聲明,那么在局部作用域中調(diào)用這個變量時,優(yōu)先調(diào)用局部作用域中聲明的變量:
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

全局變量
所有在函數(shù)外面聲明的變量都處于全局作用域中。在瀏覽器環(huán)境中,這個全局作用域就是我們的Window對象(或者整個HTML文檔)。

每一個在函數(shù)外部聲明或者定義的變量都是一個全局對象,所以這個變量可以在任何地方被使用,例如:

復(fù)制代碼 代碼如下:

// 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關(guān)鍵字,那么他自動加入到全局作用域中。

復(fù)制代碼 代碼如下:

function showAge(){
  //age初始化時沒有使用var關(guān)鍵字,所以它是一個全局變量
  age = 20;
  console.log(age);
}

showAge();  //20
console.log(age); //因為age是全局變量,所以這里輸出的也是20

setTimeout中的函數(shù)是在全局作用域中執(zhí)行的

setTimeout中的函數(shù)所處在于全局作用域中,所以函數(shù)中使用this關(guān)鍵字時,這個this關(guān)鍵字指向的是全局對象(Window):

復(fù)制代碼 代碼如下:

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)
所以的變量聲明都會提升到函數(shù)的開頭(如果這個變量在這個函數(shù)里面)或者全局作用域的開頭(如果這個變量是一個全局變量)。我們來看一個例子:

復(fù)制代碼 代碼如下:

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
}

函數(shù)聲明會覆蓋變量聲明
如果存在函數(shù)聲明和變量聲明(注意:僅僅是聲明,還沒有被賦值),而且變量名跟函數(shù)名是相同的,那么,它們都會被提示到外部作用域的開頭,但是,函數(shù)的優(yōu)先級更高,所以變量的值會被函數(shù)覆蓋掉。

復(fù)制代碼 代碼如下:

// 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

但是,如果這個變量或者函數(shù)其中是賦值了的,那么另外一個將無法覆蓋它:

復(fù)制代碼 代碼如下:

// 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

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

相關(guān)文章

  • Javascript的原型和原型鏈你了解嗎

    Javascript的原型和原型鏈你了解嗎

    這篇文章主要為大家詳細(xì)介紹了Javascript的原型和原型鏈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Js瀏覽器全屏代碼(模仿按F11)

    Js瀏覽器全屏代碼(模仿按F11)

    Js瀏覽器全屏代碼(模仿按F11),需要的朋友可以參考下。
    2011-01-01
  • 簡單了解JavaScript中的new?Function

    簡單了解JavaScript中的new?Function

    這篇文章主要介紹了簡單了解JavaScript中的new?Function,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • 十分鐘教你上手ES2020新特性

    十分鐘教你上手ES2020新特性

    這篇文章主要介紹了十分鐘教你上手ES2020新特性,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • JS如何在字符串指定位置插入字符串

    JS如何在字符串指定位置插入字符串

    文章介紹了JavaScript中如何在字符串的指定位置插入字符串,主要通過`slice`方法來實現(xiàn),`slice`方法可以根據(jù)起始和結(jié)束索引來提取字符串的一部分,文章還補(bǔ)充說明了如何在字符串的指定位置插入字符串,以及提供了相關(guān)代碼示例
    2025-01-01
  • 自定義函數(shù)實現(xiàn)IE7與IE8不兼容js中trim函數(shù)的問題

    自定義函數(shù)實現(xiàn)IE7與IE8不兼容js中trim函數(shù)的問題

    這篇文章主要介紹了自定義函數(shù)實現(xiàn)IE7與IE8不兼容js中trim函數(shù)的方法,涉及trim函數(shù)的重寫與正則匹配的技巧,需要的朋友可以參考下
    2015-02-02
  • js中獲取時間new Date()的全面介紹

    js中獲取時間new Date()的全面介紹

    下面小編就為大家?guī)硪黄猨s中獲取時間new Date()的全面介紹。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • 微信小程序?qū)W習(xí)之常用的視圖組件

    微信小程序?qū)W習(xí)之常用的視圖組件

    但是最近由于開發(fā)人手不夠,一個人開啟全棧,一邊寫接口一邊寫頁面,剛好項目中有一個需求,所以嘗試使用自定義組件開發(fā)這塊,下面這篇文章主要給大家介紹了關(guān)于微信小程序?qū)W習(xí)之常用的視圖組件的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • javascript中的鏈?zhǔn)秸{(diào)用

    javascript中的鏈?zhǔn)秸{(diào)用

    鏈?zhǔn)秸{(diào)用就是調(diào)用對象的方法后返回到該對象,嚴(yán)格來講它并不屬于語法,而只是一種語法技巧,js令人著迷的一點就是這里。
    2010-02-02
  • JavaScript實現(xiàn)矩形塊大小任意縮放

    JavaScript實現(xiàn)矩形塊大小任意縮放

    這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)矩形塊大小任意縮放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08

最新評論

尼玛县| 墨脱县| 都兰县| 额济纳旗| 社会| 罗甸县| 宝坻区| 盐山县| 海丰县| 科技| 信阳市| 洛隆县| 水富县| 陕西省| 莫力| 城步| 霍城县| 甘谷县| 读书| 双牌县| 沙雅县| 梁山县| 清河县| 巴塘县| 丹江口市| 林口县| 澄城县| 从江县| 巴中市| 大英县| 桦川县| 涪陵区| 桃园市| 涞水县| 新巴尔虎左旗| 旬邑县| 百色市| 迭部县| 姜堰市| 乐安县| 阿荣旗|