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
對於 true
和 null
關鍵字而言,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