前端错误类型

1、SyntaxError (语法错误)

输入不规范,或者变量命令等不规范。

// 缺少符号
console.log ('hello';
// Uncaught SyntaxError: missing ) after argument list

// 变量错误
// Uncaught SyntaxError: Invalid or unexpected token
var 1a = 'test'

// JSON.parse 参数不合法
// Uncaught SyntaxError: Unexpected end of JSON input
JSON.parse('')

  

2、ReferenceError (引用错误)

引用不存在的变量,将一个 undefined 变量赋值的时候,

// test 未定义,也就是未分配栈地址
// Uncaught ReferenceError: test is not defined
var t = test;

  

3、TypeError (类型错误)

// 类型调用错误
// Uncaught TypeError: Object.test is not a function
// test 未定义,应该是undefined,这里作为函数调用
Object.test()

// undefined 上面引用某一个属性
// Uncaught TypeError: Cannot read property 'a' of undefined
var test = undefined;
var t = test.a;

var test = {}
var t = test.test.a;

// null  上面引用某一个属性(虽然 null typeof 是对象,但是也会报错)
// Uncaught TypeError: Cannot read property 'a' of null
var test = null
var t = test.a

  

4、RangeError (范围越界错误)/ URIError (URI不正确) 

// Uncaught RangeError: Invalid array length
new Array(-1)

// Uncaught URIError: URI malformed
decodeURI('%dfd')

  

原文地址:https://www.cnblogs.com/jiebba/p/11314383.html