您的位置:首页 > Web前端 > JavaScript

JavaScript中的try...catch语句和es5的严格模式

2017-11-09 21:02 453 查看
try...catch语句:

try语句中出现错误时,不会执行错误后的try语句里的代码;

try语句没有出现错误,则不执行catch语句。

try{
console.log('a');
console.log(b);
console.log('c')
}catch(e){//error  error.name  error.message -->error
console.log(e.name+" : "+e.message);
}
console.log('d');
结果:
a

ReferenceError : b is not defined

d

ReferenceError:非法或不能识别的引用数值。

SyntaxError:语法解析错误

es5严格模式:

禁止使用with(),使作用域链发生修改,效率低,es5为了提升效率,严格模式下禁止使用

with(obj){...}将obj作为活动对象,放在代码块作用域的顶端,下列代码运行结果:obj   123

作用:例如document下有很多函数,每次使用都要document.,为了简便,使用

with(document){...}



禁止使用 arguments.callee(),func.caller();

拒绝重复属性和参数;

拒绝使用eval();

严格模式下变量赋值前必须声明:

b=3;//会报错

var a = b = 3;//报错

局部this必须被赋值;

"use strict";
function test(){
console.log(this);
}
test();//undefined
console.log(this);//window
补充一个知识点:
function Test(){
console.log(this);
}
new Test();//Test {} Test是构造器constructor的指向,后面再加一个{}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: