一、语法错误SyntaxError
1.1、变量名命名不规范
// JS的错误信息类型// 1、SyntaxError语法错误// 1.1 变量名命名不规范var 1 = 1; // Uncaught SyntaxError: Unexpected numbervar 1ab = 1; // Uncaught SyntaxError: Invalid or unexpected token
1.2、不能给关键字赋值
// 1.2 不能给关键字赋值new = 5; // Uncaught SyntaxError: Unexpected token '='function = 1; // Uncaught SyntaxError: Unexpected token '='
1.3、基本的语法错误
var a = 5: // Uncaught SyntaxError: Unexpected token ':'function 1test(){} // Uncaught SyntaxError: Invalid or unexpected token
二、ReferenceError引用错误
2.1、变量未被声明就使用
// 2、ReferenceError引用错误// 2.1、变量未被声明就使用test(); // Uncaught ReferenceError: test is not definedconsole.log(a); // Uncaught ReferenceError: a is not defined
三、给无法赋值的对象赋值
// 2.2、给无法赋值的对象赋值var a = 1 = 2; // Uncaught SyntaxError: Invalid left-hand side in assignmentvar b = 1;console.log(b) = 1; // Uncaught ReferenceError: Invalid left-hand side in assignment
四、RangeError范围错误
var arr = [1, 2, 3, 4];arr.length = 5; // [1, 2, 3, 4, empty]arr.length = -1; // Uncaught RangeError: Invalid array lengthconsole.log(arr);
var num = new Number(66.66);console.log(num.toFixed(-1)); // Uncaught RangeError: toFixed() digits argument must be between 0 and 100 at Number.toFixed
五、TypeError类型错误
5.1、调用不存在的方法
// 5、TypeError类型错误// 5.1、调用不存在的方法123(); // Uncaught TypeError: 123 is not a functionvar obj = {};obj.say(); // Uncaught TypeError: obj.say is not a functionvar a = new 'string'; // Uncaught TypeError: "string" is not a constructorvar b = new 123; // Uncaught TypeError: 123 is not a constructor
六、URIError
- URI:Uniform Resource Identifier 统一资源标识符
- URL:Uniform Resource Location 统一资源定位符
URN:Uniform Resource Name 统一资源名称
// 六、URI错误// URI:Uniform Resource Identifier 统一资源标识符// URL:Uniform Resource Location 统一资源定位符// URN:Uniform Resource Name 统一资源名称url:http://www.baidu.com/newListurn:www.baidu.com/ftp#developer 资源的唯一性ID
七、encodeURI和decodeURI
encodeURI:把中文转为中文编码
decodeURI:把中文编码转为中文
// encodeURI:把中文转为中文编码// decodeURI:把中文编码转为中文var myUrl = 'http://www.baidu.com?name="良雨"';var newUrl = encodeURI(myUrl);console.log(newUrl); // http://www.baidu.com?name=%22%E8%89%AF%E9%9B%A8%22var url = decodeURI(newUrl);console.log(url); // http://www.baidu.com?name="良雨"// 不能自己随意写编码,否则会报错var str = decodeURI('%fauflhga');console.log(str); // Uncaught URIError: URI malformed at decodeURI
八、JSON对象
JSON对象属性名最好是使用双引号,因为后端传递过来的JSON基本上也是双引号
- JSON对象里面是不可以有方法的
疑惑:JSON里面有方法,竟然还能执行,竟然不报错
// JSON对象// JSON对象属性名最好是使用双引号,因为后端传递过来的JSON基本上也是双引号// JSON对象里面是不可以有方法的var obj = {"a": 1,"b": 2,"say": function(){console.log(1);}}obj.say();
九、EvalError
9.1、eval可以把JSON字符串变为对象
有报错
var jsonData = '[''{'+'"name": "abc"'+'},'+'{'+'"name": "dce"'+'},'+'{'+'"name": "efg"'+'}'+']';var data = eval('('+ jsonData +')'); // 报错for(var i in data){var item = data[i];console.log(item);}
2、为什么不推荐用eval
语法不规范,不加字符串也可以执行
- eval不好调试,比如在断点调试的时候
- 有些许的性能问题
- 代码在压缩时,eval可能会报错
- 有些许的安全问题,容易遭到xss攻击
-
十、try catch / finally / throw手动抛出错误
10.1、try catch / finally
try里有没有错误,finally里面都会执行
try里有没有错误,都不影响外面的代码执行
// try catch / finally / throw 手动抛出错误try{console.log('正常执行1');console.log(a); // 不会报错console.log('正常执行2');}catch(e){console.log(e); // ReferenceError: a is not definedconsole.log(e.name); // ReferenceErrorconsole.log(e.message); // a is not defined}finally{console.log('正常执行3'); // try里有没有错误,finally里面都会执行}console.log('try里有没有错误,都不影响外面的代码执行'); // try里有没有错误,都不影响外面的代码执行
10.2、throw手动抛出错误
// throw手动抛出错误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);}
十一、ES5的严格模式
11.1、ECMAScript的版本历程
1997年 1.0
- 1998年 2.0
- 1999年 3.0 JS通行标准
- 2007年 4.0 草案 争议比较大
- 2008年 4.0 终止 把容易改善的部分划分为3.1 不容易改善的部分划分为Harmony ES5草案
- 2009年 5.0 发布 Harmony一分为二,一半为JS.next; 一半为JS.next.next
- 2011年 5.1 ISO国际标准
- 2013年 ES6其实就是JS.next;JS.next.next划分为ES7
-
11.2、严格模式的使用
'use strict'; // 不推荐这样使用function(){'use strict'}var test = (function(){'use strict'; // 推荐这样使用})();
11.3、启用严格模式的一些状况
11.3.1、启用严格模式后with将不能被使用
with可以改变作用域
// 启用严格模式后,with将不能被// with可以改变作用域'use strict'; // Uncaught SyntaxError: Strict mode code may not include a with statementvar a = 1;var obj = {a: 2}function test(){var a = 3;with(window){console.log(a); // 1}}test();
11.3.2、启用严格模式后callee和caller将不能被使用
// 启用严格模式后callee和caller将不能被使用// 启用严格模式后callee不能被使用'use strict'; // Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to themfunction test1(){console.log(arguments.callee);}test1();// 启用严格模式后caller不能被使用'use strict'; // Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to themfunction test1(){test2();}test1();function test2(){console.log(test2.caller);}
11.3.3、严格模式下变量必须声明后才能使用
// 严格模式下,变量必须声明后才能使用'use strict';a = 1; // Uncaught ReferenceError: a is not defined
11.3.4、严格模式下函数内部this指向的问题
严格模式下,函数内部this必须赋值,否则this将指向undefined
// 严格模式下函数内部的this必须赋值,否则将指向undefined'use strict';function test(){console.log(this);}test();test.call({});var test1 = new test();
11.3.5、严格模式下函数的形参不能重复「会报错」
// 严格模式下,函数的形参不能重复「会报错」'use strict';function test(a, a, b){console.log(a, b); // Uncaught SyntaxError: Duplicate parameter name not allowed in this context}test(1, 2, 3);
11.3.6、严格模式下对象的属性名不能重复「不会报错」
// 严格模式下,对象的属性名不能重复「不会报错」'use strict';var obj = {a: 11,a: 22}console.log(obj.a);
11.3.7、严格模式下eval是有自己的作用域的
// 严格模式下,eval是有自己的作用域的'use strict';eval("var a = 1; console.log(a)");console.log(a); // Uncaught ReferenceError: a is not defined
