<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      javascript中的變量作用域以及變量提升

      在javascript中, 理解變量的作用域以及變量提升是非常有必要的。這個(gè)看起來(lái)是否很簡(jiǎn)單,但其實(shí)并不是你想的那樣,還要一些重要的細(xì)節(jié)你需要理解。

      變量作用域

      “一個(gè)變量的作用域表示這個(gè)變量存在的上下文。它指定了你可以訪(fǎng)問(wèn)哪些變量以及你是否有權(quán)限訪(fǎng)問(wèn)某個(gè)變量。”

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

      局部變量(處于函數(shù)級(jí)別的作用域)

      不像其他對(duì)面對(duì)象的編程語(yǔ)言(比方說(shuō)C++,Java等等),javascript沒(méi)有塊級(jí)作用域(被花括號(hào)包圍的);當(dāng)是,javascript有擁有函數(shù)級(jí)別的作用域,也就是說(shuō),在一個(gè)函數(shù)內(nèi)定義的變量只能在函數(shù)內(nèi)部訪(fǎng)問(wèn)或者這個(gè)函數(shù)內(nèi)部的函數(shù)訪(fǎng)問(wèn)(閉包除外,這個(gè)我們過(guò)幾天再寫(xiě)個(gè)專(zhuān)題)。

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

      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

      沒(méi)有塊級(jí)作用域:

      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)鍵字
          如果聲明一個(gè)變量的時(shí)候沒(méi)有使用var關(guān)鍵字,那么這個(gè)變量將是一個(gè)全局變量!
      // 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);
      }
          局部變量?jī)?yōu)先級(jí)大于全局變量
      如果在全局作用域中什么的變量在局部作用域中再次聲明,那么在局部作用域中調(diào)用這個(gè)變量時(shí),優(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)境中,這個(gè)全局作用域就是我們的Window對(duì)象(或者整個(gè)HTML文檔)。

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

      // name and sex is not in any function
      var myName = "zhou";
      var sex = "male";
       
      //他們都處在window對(duì)象中
      console.log(window.myName); //paul
      console.log('sex' in window); //true

      如果一個(gè)變量第一次初始化/聲明的時(shí)候沒(méi)有使用var關(guān)鍵字,那么他自動(dòng)加入到全局作用域中。

      function showAge(){
        //age初始化時(shí)沒(méi)有使用var關(guān)鍵字,所以它是一個(gè)全局變量
        age = 20;
        console.log(age);
      }
       
      showAge();  //20
      console.log(age); //因?yàn)閍ge是全局變量,所以這里輸出的也是20

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

      setTimeout中的函數(shù)所處在于全局作用域中,所以函數(shù)中使用this關(guān)鍵字時(shí),這個(gè)this關(guān)鍵字指向的是全局對(duì)象(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
      為了避免對(duì)全局作用域的污染, 所以一般情況下我們盡可能少的聲明全局變量。 

       

      變量提升(Variable Hoisting)

      所以的變量聲明都會(huì)提升到函數(shù)的開(kāi)頭(如果這個(gè)變量在這個(gè)函數(shù)里面)或者全局作用域的開(kāi)頭(如果這個(gè)變量是一個(gè)全局變量)。我們來(lái)看一個(gè)例子:

      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ù)聲明會(huì)覆蓋變量聲明

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

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

       

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

      // 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
       
      最后一點(diǎn), 在嚴(yán)格模式下,如果沒(méi)有先聲明變量就給變量賦值將會(huì)報(bào)錯(cuò)!

       

      posted @ 2013-10-23 23:29  CodingMonkey  閱讀(3864)  評(píng)論(2)    收藏  舉報(bào)
      主站蜘蛛池模板: 中文字幕av日韩有码| 五月天丁香婷婷亚洲欧洲国产| 曲阜市| 亚洲第一国产综合| 精品夜恋影院亚洲欧洲| 国产精品中文字幕日韩| 午夜福利在线观看6080| 好看的国产精品自拍视频| 国产精品普通话国语对白露脸| 台东县| 午夜视频免费试看| 午夜三级成人在线观看| 婷婷五月综合丁香在线| 青青青久热国产精品视频| 免费国产又色又爽又黄的网站| 亚洲乱码中文字幕小综合| 亚洲男女羞羞无遮挡久久丫| 日韩av一区二区三区在线| 92国产精品午夜福利免费| 国产午夜精品一区二区三区漫画| 露脸一二三区国语对白| 日韩av片无码一区二区不卡| 中文字幕熟妇人妻在线视频| 国产欧美精品一区aⅴ影院| 亚洲av激情综合在线| 综合色一色综合久久网| 亚洲夂夂婷婷色拍ww47| 国产又粗又猛又爽又黄| 少妇撒尿一区二区在线视频| 在国产线视频A在线视频| 青青草无码免费一二三区| 国产成人av一区二区三| 性色在线视频精品| 国产亚洲真人做受在线观看| 精品国产品香蕉在线| 蒲江县| 亚洲老女人区一区二视频| 国产综合久久亚洲综合| 国产99视频精品免费视频36 | 国产一卡2卡三卡4卡免费网站| 国产精品午夜福利视频|