javascript塊級範圍

來源:互聯網
上載者:User
var x=1;

(function(){

var x="inner";

alert(x);

var x = "undefined";

})();

alert(x);

我現在只知道,在(function(){....})()這樣的結構中,會產生塊級範圍。我的理解是只在那一個函數中,首先,在外面聲明一個全域變數,x=1

那麼最後的alert(x),輸出也是1。

在函數裡,假如沒有聲明x="inner",alert(x)就會彈出undefined,注意不是彈出下面的x="undefined"。很簡單吧,是的,水平就這樣。

typeof

typeof 運運算元可按下列兩種方式來使用︰

1. typeof operand2. typeof (operand)

typeof 運運算元可返回字串,這些字串指出未求值的運算元的類型。operand 是指字串、變數、關鍵字、物件,其類型可被 typeof 返回。括弧並非必要的。

假設你定義了以下變數︰

var myFun = new Function("5+2")var shape="round"var size=1var today=new Date()

typeof 運運算元對以下變數返回以下結果︰

typeof myFun 返回的是 functiontypeof shape 返回的是 stringtypeof size 返回的是 numbertypeof today 返回的是 objecttypeof dontExist 返回的是 undefined

對於 truenull 關鍵字而言,typeof 運運算元返回以下結果︰

typeof true 返回的是 booleantypeof null 返回的是 object

對於數字或字串而言,typeof 運運算元返回以下結果︰

typeof 62 返回的是 numbertypeof 'Hello world' 返回的是 string

對於屬性值而言,typeof 運運算元返回屬性裡的值的類型︰

typeof document.lastModified 返回的是 stringtypeof window.length 返回的是 numbertypeof Math.LN2 返回的是 number

對於方法和函數而言,typeof 運運算元返回以下結果︰

typeof blur 返回的是 functiontypeof eval 返回的是 functiontypeof parseInt 返回的是 functiontypeof shape.split 返回的是 function

對於預先定義的物件而言,typeof 運運算元返回以下結果︰

typeof Date 返回的是 functiontypeof Function 返回的是 functiontypeof Math 返回的是 functiontypeof Option 返回的是 functiontypeof String 返回的是 function
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.