js-try catch、ES5.0严格模式

js-try catch、ES5.0严格模式

try...catch

  • try{}catch{}finally{},用以捕捉错误而不影响后面代码的执行。即try里面的代码如果报错了,不会影响try catch后面代码块的执行。
try{
      console.log('a');//a
      console.log(b);//try里面的代码
      console.log('c');
    }catch(e){
      console.log(e.name + ":" + e.message);// ReferenceError:'b' is not defined
    }
    console.log('d');//d
  • Error.name的六种值对应信息
    • EvalError: eval()的使用与定义不一致
    • RangeError: 数值越界
    • ReferenceError: 非法或不能识别的引用数值
    • SyntaxError:发生语法解析错误
    • TypeError: 操作数类型错误
    • URIErroe:URI处理函数使用不当

ES 5.0标准模式

  • ”use strict“
    • 不再兼容ES3的一些不规则语法。使用全新的ES5规范。
    • 两种方法:
      • 全局严格模式--必须写在页面的逻辑最顶端
      • 局部函数严格模式 -- 必须写在函数的逻辑最顶端
    • 就是一行字符串,不会对不兼容严格模式的浏览器产生影响.
    • 特点:
      • 不支持with, arguments.callee, func. caller,
      • 变量赋值前必须声明,
      • 局部this必须被赋值(Person.call(null/undefined)赋值什么就是什么),
      • 拒绝重复属性和参数。//但不报错
//ES5.0严格模式的启动
    //要写在页面逻辑最顶端
    "use strict";
    function test(){
      console.log(arguments.callee);//报错,严格模式不能用callee
    }
//局部的严格模式要下载函数的最顶端
function test(){
	"use strict";
    console.log(arguments.callee);//报错,严格模式不能用callee
    }
原文地址:https://www.cnblogs.com/1549983239yifeng/p/14431116.html