天天看點

JavaScript深入淺出之語句

block語句:

塊語句常用于組合0~多個語句,塊語句用一對花括号定義。

{a:1, b:2}//SyntaxError: Unexpected token;
var o={a:1, b:2};
           

注:沒有塊級作用域

for(var i=0; i<10; i++) {//i為全局變量
    var str="hi";
    console.log(str);
}
           
var i=0;
for(; i<10; i++) {
    var str="hi";
    console.log(str);
}
           

以上兩段代碼等價。再來一個例子:

function foo() {
    var a=1;
    console.log(a);//1
}
foo();
console.log(typeof a);//undefined
           

var語句

function foo() {
    var a=b=1;//其中a為局部變量,b為隐式全局變量
    //var a=1, b=1;//其中a、b都為局部變量
}
foo();
console.log(typeof a);//undefined
console.log(typeof b);//number
           

try catch語句

提供異常捕獲機制,三種形式:

try {//第一種
    throw "test";
} catch(ex) {
    console.log(ex);//test
} finally {
    console.log("finally");//finally
}
           
try{//第二種
    //do sth
} finally {
    console.log("finally");
}
           
try {//第三種
    throw "test";
} catch(ex) {
    console.log(ex);//test
}
           

try catch語句的嵌套:

try{//例一
    try{
        throw new Error("oops");
    } finally {
        console.log("finally");
    }
} catch(ex) {
    console.error("outer", ex.message);
}//執行結果:finally outer oops
           
try{//例二
    try{
        throw new Error("oops");
    } catch(ex) {
        console.error("inner", ex.message);
    } finally {
        console.log("finally");
    }
} catch(ex) {
    console.error("outer", ex.message);
}//執行結果:inner oops finally
           
try{//例三
    try{
        throw new Error("oops");
    } catch(ex) {
        console.error("inner", ex.message);
        throw ex;
    } finally {
        console.log("finally");
    }
} catch(ex) {
    console.error("outer", ex.message);
}//執行結果:inner oops finally outer oops
           

總結:

  • 内部異常處理沒被catch捕獲,才會抛出給外部的catch;
  • 内部異常被catch捕獲後再抛出異常,外部的catch也能夠捕獲;
  • 内部異常被catch捕獲後未抛出,外部catch不捕獲異常。

function語句

用來定義函數對象。

fd();//true,函數聲明會被預先處理,或叫函數前置
function fd() {//函數聲明
    //do sth
    return true;
}
           
fe();//TypeError
var fe = function() {//函數表達式
    //do sth
};
           

for……in語句

var p;
var obj = {x:1, y:2}
for(p in obj) {//周遊obj中的屬性
}
           
  • 順序不确定,順序的确定依賴于引擎的實作。如果想要按照順序對數組或對象的屬性進行周遊的話,不要使用for…in。
  • 每一個對象的屬性都是有屬性描述器的,如果它的enumerable為false的話,不會在for…in中出現。
  • for…in對象屬性受原型鍊的影響。如果一個對象的原型鍊上的原型有其他的屬性。

switch語句

switch(val) {
    case 1:
        console.log(1);
        break;
    case 2:
        console.log(2);
        break;
    default:
        console.log(0);
        break;
}//2
           
case 1:
        console.log(1);
    case 2:
        console.log(2);
    default:
        console.log(0);
}//2 0
           
switch(val) {
    case 1:
    case 2:
    case 3:
        console.log(123);
        break;
    case 4:
    case 5:
        console.log(45);
        break;
    default:
        console.log(0);
}//123
           

循環語句

while(isTrue) {
    //do sth
}

do {
    //do sth
} while(isTrue)

for(var i=0; i<n; i++) {
    //do sth
}
           

with語句

修改目前作用域

with({x: 1}) {
    console.log(x);//1
}
           
with(document.forms[0]) {
    console.log(name.value);
}

var form = document.forms[0];
console.log(form.name.value);//與上面的代碼一樣
           

缺點:

  • 讓JS引擎優化更難
  • 可讀性差
  • 嚴格模式下禁用
  • 可以通過定義變量來取代(需要使用深層通路with對象的時候 )

繼續閱讀