JS 错误类

背景

最近 在做 JS 笔试题的时候 一直分不清楚每个错误类 所以在查阅资料后 写下了这篇笔记

SyntaxError

SyntaxError 是解析代码时发生的语法错误

// 变量名错误
var 1a;

// 缺少括号
console.log 'hello');

ReferenceError

ReferenceError 是引用一个不存在的变量时发生的错误

// 比如我们直接打印一个未声明的变量
console.log(unknownVariable) // ReferenceError: unknownVariable is not defined

// 另一种触发场景是,将一个值分配给无法分配的对象,比如对函数的运行结果或者this赋值。

console.log() = 1 // ReferenceError: Invalid left-hand side in assignment

this = 1 // ReferenceError: Invalid left-hand side in assignment

// 上面代码对函数console.log的运行结果和this赋值,结果都引发了ReferenceError错误

RangeError

RangeError 是当一个值超出有效范围时发生的错误。主要有几种情况,一是数组长度为负数,二是 Number 对象的方法参数超出范围,以及函数堆栈超过最大值

TypeError

TypeError 是变量或参数不是预期类型时发生的错误。比如,对字符串、布尔值、数值等原始类型的值使用 new 命令,就会抛出这种错误,因为 new 命令的参数应该是一个构造函数

new 123(); //TypeError: number is not a func

URIError

URIError 是 URI 相关函数的参数不正确时抛出的错误,主要涉及 encodeURI()、decodeURI()、encodeURIComponent()、decodeURIComponent()、escape()和 unescape()这六个函数

decodeURI('%2'); // URIError: URI malformed

Last updated

Was this helpful?