错误信息

1、SyntaxError 语法错误

例如变量名以数字开头
image.png

关键字赋值
image.png

基本语法错误
image.png

2、引用错误

变量或者函数未被声明然后直接调用
image.png
image.png

给无法赋值的数据进行赋值
image.png

3、RangeError 范围错误

数组长度为负数的时候
image.png

4、TypeError 类型错误

调用不存在的方法
image.png

实例化原始值
image.png

5、URIError

image.png

6、eval() 函数执行错误

自定义错误类型

  1. new Error("代码错误");
  2. // Error: 代码错误
  3. // at <anonymous>:1:1

try…catch…finally…

try代码块内发生报错,程序不会停止执行会直接到catch中继续执行
catch代码块的主要作用就是捕获try块中的错误然后执行相关的逻辑
finally不管try有没有报错,也不管catch中有什么处理逻辑,finally内都会执行

  1. try {
  2. console.log("正常执行1");
  3. console.log(a);
  4. console.log("正常执行2");
  5. } catch (error) {
  6. console.log(error); // 字符串的报错提示
  7. console.log(error.name); // 错误的类型
  8. console.log(error.message); // 错误的具体的错误内容
  9. console.log("正常执行3");
  10. } finally {
  11. // 不管try有没有错误,不管catch里面有什么操作,finally都会执行
  12. console.log("正常执行4");
  13. }
  14. console.log("正常执行5");
  15. // 正常执行1
  16. // VM9547:7 ReferenceError: a is not defined
  17. // at <anonymous>:3:17
  18. // ReferenceError
  19. // a is not defined
  20. // 正常执行3
  21. // 正常执行4
  22. // 正常执行5

还可以配合throw来抛出一场信息。

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

**throw**直接抛出一个字符串错误的时候,是拿不到**name****message**属性的。

  1. var jsonStr = "";
  2. try {
  3. if (jsonStr === "") {
  4. throw "JSON字符串为空";
  5. }
  6. } catch (error) {
  7. console.log(error);
  8. console.log(error.name);
  9. console.log(error.message);
  10. }
  11. // JSON字符串为空
  12. // undefind
  13. // undefind
  1. var jsonStr = "";
  2. try {
  3. if (jsonStr === "") {
  4. throw new Error("JSON字符串为空");
  5. }
  6. } catch (error) {
  7. console.log(error);
  8. console.log(error.name);
  9. console.log(error.message);
  10. }
  11. // Error: JSON字符串为空
  12. // at <anonymous>:4:11
  13. // Error
  14. // JSON字符串为空

严格模式

JavaScript是由ECMAScriptDOMBOM三部分组成的,「严格模式」指的是ECMAScript5.0版本后的语法、方法规范。

:::info ECMAScript的历史

  • 1997年,1.0 版本发布
  • 1998年,2.0 版本发布
  • 1999年,3.0版本发布
  • 2007年,4.0提出草案,但是因为技术太过激进,多数浏览器厂商不同意这个草案
  • 2008年,4.0版本中止,部分规范搬到 3.1 版本中,后 3.1 版本直接更名为 5.0 版本发布
  • 2009年,5.0版本发布,5.0 版本没有对 3.0 版本进行太大的改动
  • 2011年,5.1版本发布
  • 2013年,6.0提出草案
  • 2015年,6.0版本发布 :::

当时的ECMAScript3.0版本的语言安全性比较低,且效率一般,另外加上ECMAScript5.0大火,所以新增「严格模式」。
某些 3.0 语法(包括非 3.0 语法)在正常模式下运行正常,但是在ES5的严格模式下运行就会报错或者运行不正常。

启动「严格模式」的方式是使用"use strict"字符串。

  1. // 启用全局严格模式
  2. "use strict";
  3. function test() {
  4. // 函数内启用严格模式
  5. "use strict";
  6. }

举例子🌰 :
正常模式下,a直接赋值属于window对象的属性,严格模式下a会提示未定义。

  1. "use strict";
  2. // Uncaught ReferenceError: a is not defined
  3. a = 1;

正常模式下,函数内部的this指向window对象,严格模式下this对象是undefind

  1. "use strict";
  2. function test(){
  3. console.log(this); // undefined,非严格模式下 this 指向 window
  4. }
  5. test();

正常模式下,console.log(a)正常输出1,严格模式下提示a是未定义。

  1. "use strict";
  2. eval("var a = 1; console.log(a);");
  3. // a is not defined,严格模式下 eval 是有独立的作用域
  4. console.log(a);