JavaScript深入浅出之语句

本文深入探讨JavaScript中的语句,包括block、var、try catch、function、for...in、switch、循环和with语句。强调了try catch的异常处理机制、for...in的注意事项以及with语句的缺点和替代方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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对象的时候 )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值