1. JS错误信息

1.1 SyntaxError 语法错误

  1. // 1.变量名不规范
  2. var 1 = 1;
  3. var 1a = 1;
  4. // 2.关键字不可以赋值
  5. var new = 1; // Uncaught SyntaxError: Unexpected token 'new'
  6. // 3.基本语法错误
  7. var a = 6: // Uncaught SyntaxError: Unexpected token ':'

1.2 ReferenceError 引用错误

  1. // 1. 变量或函数未声明
  2. test(); //Uncaught ReferenceError: test is not defined
  3. console.log(a); // Uncaught ReferenceError: a is not defined
  4. // 2.给无法赋值的对象赋值的时候
  5. var a = 1 = 2;// Uncaught SyntaxError: Invalid left-hand side in assignment

1.3 RangeError 范围错误

  1. // 1. 数组长度超出可行范围
  2. var arr = [1, 2, 3];
  3. arr.length = 1; // Uncaught RangeError: Invalid array length
  4. // 2.对象方法超出可行范围
  5. var num = new Number(66.66);
  6. console.log(num.toFixed(150));
  7. // Uncaught RangeError: toFixed() digits argument must be between 0 and 100

1.4 TypeError 类型错误

  1. // 1.调用不存在的方法
  2. var obj = {};
  3. obj.say();// Uncaught TypeError: obj.say is not a function
  4. // 2.实例化原始值
  5. var a = 1; // Uncaught TypeError: 1 is not a constructor
  6. var a = new false; // Uncaught TypeError: false is not a constructor

1.5 URIError URI错误

  1. // ? URI : Uniform Resource Identifier
  2. // ? 统一资源标识符
  3. // ? URL : Uniform Resource Locator
  4. // ? 统一资源定位符
  5. // ? URN : Uniform Resource Name
  6. // ? 统一资源名称
  7. var str = decodeURI('%dasaf%'); // Uncaught URIError: URI malformed

1.6 EvalError eval函数执行错误

JSON 与对象

  1. var jsonData = '['+
  2. '{'+
  3. '"name": "abc"'+
  4. '},'+
  5. '{'+
  6. '"name": "bcd"'+
  7. '},'+
  8. '{'+
  9. '"name": "efg"'+
  10. '}'+
  11. ']';
  12. console.log(jsonData);
  13. // ? json字符串 => json对象
  14. var data = eval('('+ jsonData + ')');
  15. console.log(data);
  16. for(var i in data){
  17. var item = data[i];
  18. console.log(item.name);
  19. }

image.png

1.7 Error

  1. var error = new Error('代码错误了');
  2. var sError = new SyntaxError('代码错误了');
  3. var rError = new ReferenceError('代码错误了');
  4. var tError = new TypeError('代码错误了');
  5. console.log(error);
  6. console.log(sError);
  7. console.log(rError);
  8. console.log(tError);

image.png

2. try-catch

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

image.pngimage.png

  1. var jsonStr = '';
  2. try{
  3. if(jsonStr == ''){
  4. throw 'JSON字符串为空';
  5. }
  6. console.log('我要执行了!!!!!');
  7. var json = JSON.parse(jsonStr);
  8. console.log(json);
  9. }catch(e){
  10. console.log(e);
  11. var errorTip = {
  12. name: '数据传输失败',
  13. errorCode: '10010'
  14. }
  15. console.log(errorTip);
  16. }

image.png

3. 严格模式

  1. ECMAScript JavaScript语法规范, 方法规范<br /> 97 1.0<br /> 98 2.0<br /> 99 3.0 JS通行标准<br /> 07 4.0草案 <br /> 08 4.0中止 容易改善3.1 激进部分Harmony<br /> 更名ECMAScript5<br /> 09 5.0发布, Harmony => JS.NEXT, JS.next,next<br /> 11 5.1 ISO国际标准<br /> 13 ES6 = js.next js.next.next 7<br /> 13 ES6草案发布<br /> 15 ES6正式发布, ECMAScript2015

3.1 with不可以使用

  1. // ES5 分为 正常模式 ,严格模式
  2. // IE9及以下IE 不支持
  3. // 3.0 => 严格模式
  4. // 1.with不可以使用
  5. 'use strict';
  6. var namespace = {
  7. header: {
  8. Jenny: {
  9. a: 1,
  10. b: 2
  11. },
  12. Ben: {
  13. a: 3,
  14. b: 4
  15. }
  16. },
  17. sideBar: {
  18. Crystal: {
  19. a: 5,
  20. b: 6
  21. }
  22. }
  23. }
  24. with(namespace.header.Ben){
  25. console.log(a);
  26. }

image.png

3.2 caller callee 不可以使用

  1. 'use strict';
  2. function test(){
  3. // console.log(arguments.callee);
  4. test2();
  5. }
  6. function test2(){
  7. console.log(test2.caller);
  8. }
  9. test();
  1. 'use strict';
  2. function test(){
  3. console.log(arguments.callee);
  4. }
  5. test();

image.png

3.3 不用var声明报错

  1. 'use strict';
  2. a = 1;

image.png

3.4 全局的this指向window, 函数内部的指向undefined

  1. 'use strict';
  2. function test(){
  3. console.log(this);
  4. }
  5. console.log(this); // window{}
  6. test(); // undefined

3.5 非严格模式.call(1) 会包装类

  1. 'use strict'
  2. function test(a){
  3. console.log(this);
  4. }
  5. test.call(1, 2);

image.pngimage.png

3.6 函数的参数不能重复

  1. 'use strict'
  2. function test(a, a){
  3. console.log(a);
  4. }
  5. test(1, 2);

image.png