js的各种错误类型

1.SyntaxError(语法错误)

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

eg:var 1a;

  Uncaught SyntaxError: Unexpected number

2.ReferenceError(引用错误)

a.引用了一个不存在的变量

eg: console.log(a);

  Uncaught ReferenceError: a is not defined

b.将变量赋值给一个无法被赋值的对象

eg:console.log()= 1;

  Uncaught ReferenceError: Invalid left-hand side in assignment

3.RangeError(范围错误)

超出有效范围

eg:var a= new Array(-1);

  Uncaught RangeError: Invalid array length

4.TypeError(类型错误)

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

eg: var a= new 123;

  Uncaught TypeError: 123 is not a function

b.调用对象不存在的方法

eg:var a;a.aa();

  Uncaught TypeError: Cannot read property 'aa' of undefined

  

5.URLError(URL错误)

与url相关函数参数不正确,主要是encodeURI()、decodeURI()、encodeURIComponent()、decodeURIComponent()、escape()和unescape()这六个函数。

eg: decodeURI('%2')

  Uncaught URIError: URI malformed

6.EvalError(eval错误)

eval函数没有被正确执行

原文地址:https://www.cnblogs.com/yanze/p/5997489.html