1.错误信息

  1. //1.SyntaxError 语法错误
  2. var 1 = 1;
  3. var 1ab = 1;
  4. // 关键字赋值
  5. new = 5;
  6. function = 1;
  7. // 基本的语法错误
  8. var a = 5;
  9. // 2.ReferenceError
  10. //变量或者函数未被声明
  11. test();
  12. //给无法被赋值的对象赋值的时候
  13. var a = 1 =2;
  14. var a = 1;
  15. console.log(a) = 1;
  16. // JS错误信息类型
  17. // 3.RangeError 范围错误
  18. // 数组长度赋值为负数
  19. var arr = [1,2,3];
  20. arr.length=-1;
  21. console.log(arr);
  22. // 对象方法参数超出可行范围
  23. var num = new Number(66.66);
  24. console.log(num.toFixed(-1));
  25. // 4.TypeError 类型错误
  26. // 调用不存在的方法
  27. 123();
  28. var obj = {};
  29. // obj.say 不会报错因为浏览器会认为是一个属性没有赋值但是obj.say()报错是一个属性不是fn(){}
  30. obj.say();
  31. // 实例化原始值
  32. var a = new 'string';
  33. var a = new 123;
  34. // 5. URIError URI错误
  35. //URI: UNIFError URI错误
  36. //URI:UNIFORM RESOURCE IDENTIFIER
  37. // 统一资源标识符
  38. //URN: UNIFORM RESOURCE LOCATOR
  39. // 统一资源定位符
  40. //URL: UNIFORM RESOURCE NAME
  41. // 统一资源名称
  42. //URL:http://www.baidu.com/news#today
  43. // ftp://www.baidu.com/ftp#developer
  44. //URN: www.baidu.com/ftp#developer -> ID
  45. // href="tel:13900000000"
  46. // href='mailto:523579987@qq.com'
  47. var myUrl = 'http://www.baidu.cin?name=艾小野';
  48. var newUrl = encodeURI(myUrl);
  49. console.log(newUrl);
  50. var newNewUrl = decodeURI(newUrl);
  51. console.log(newNewUrl);
  52. //6.EvalError
  53. 创建一个error实例,表示错误的原因:与 eval() 有关。

2.try_catch

try语句包含了由一个或者多个语句组成的try块, 和至少一个catch块或者一个finally块的其中一个,或者两个兼有, 下面是三种形式的try声明:

  1. try...catch
  2. try...finally
  3. try...catch...finally

catch子句包含try块中抛出异常时要执行的语句。也就是,你想让try语句中的内容成功, 如果没成功,你想控制接下来发生的事情,这时你可以在catch语句中实现。 如果在try块中有任何一个语句(或者从try块中调用的函数)抛出异常,控制立即转向catch子句。如果在try块中没有异常抛出,会跳过catch子句。
finally子句在try块和catch块之后执行但是在下一个try声明之前执行。无论是否有异常抛出或捕获它总是执行。

    try{
        console.log('正常执行1');
        console.log('a');
        console.log('b');
        console.log('正常执行2');
    }catch(e){
        console.log(e.name+':'+e.message);
    }finally{
        console.log('正常执行3');
    }
    console.log('正常执行4');

    var jsonStr = '';
    try{
        if(jsonStr == ''){
            throw 'JSON字符串为空';
        }
        console.log('我要执行了');
        var json = JSON.parse(jsonStr);
        console.log(json);
    }catch(e){
        console.log(e);
        var errorTip ={
            name:'数据传输失败',
            errorCode:'10010'
        }
        console.log(errorTip);
    }

image.png

3.严格模式

ES5 正常模式 严格模式<br />    IE9及以下IE<br />    3.0 -> 严格模式
//严格模式的三种写法
        'use strict'; //全局写法

    function test() {
        'use strict';  //局部函数写法
    }

    var test = (function () {
        'use strict'; //局部函数写法
    })();

caller callee 严格模式下不可用

    'use strict';
        function test1(){
        test2();
    }
    test1();
    function test2(){
        console.log(test2.caller);
    }

with 改变作用域 严格模式下不可用

  非严格模式下  
    var a = 1;
    var obj ={
        a:2
    }
    function test() {
        var a = 3;
        with(window){
            console.log(a); //1
        }
    }
    test();
    严格模式下
  报错

解决命名空间重复的问题

    window.onload = function () {
        init();
    }

    function init() {
        initSlider;
        initSideBar;
    }
    var initSlider = (function () {
        var a = 1;
        console.log(a);
    })();

    var initSideBar = (function () {
        var a = 2;
        console.log(a);
    })();

以前的解决方法

    var namespace = {
        header: {
            Jenny: {
                a: 1,
                b: 2
            },
            Ben: {
                a: 3,
                b: 4
            }
        },
        sideBar: {
            Crystal: {
                a: 5,
                b: 6
            }
        }
    }
    with(namespace.header.Ben){ //**
        console.log(a);
    }
         //严格模式下
    'use strict';
        a = 1;  //报错
         var a = b = 1; //报错
    function test(){
        console.log(this);  //严格模式下this必须声明
    }
    test();//undefined
    test.call(1); // 1

    // 函数的参数不能重复
    function test(a,a){
        console.log(a);
    }
    test(1,2);

        //不会报错
    var obj = {
        a:1,
        a:2
        }
    console.log(obj.a);

    eval('var a = 1;console.log(a)');  //非严格模式下 eval是全局windows 
                                                                            //严格模式下 eval有自己的作用域
    console.log(a); //报错

         //非严格模式下
        a = 1;  //报错
         var a = b = 1; //报错
    function test(){
        console.log(this);  //严格模式下this必须声明
    }
    test();//window
    test.call(1); // Number{1}