引用一个不存在的变量时发生的错误。将一个值分配给无法分配的对象
// 2.1 引用了不存在的变量 a() // Uncaught ReferenceError: a is not defined console.log(b) // Uncaught ReferenceError: b is not defined // 2.2 给一个无法被赋值的对象赋值 console.log("abc") = 1 // Uncaught ReferenceError: Invalid left-hand side in assignmentRangeError是当一个只超出有效范围时发生的错误。主要的有几种情况,第一是数组长度为负数,第二是Number对象的方法参数超出范围,以及函数堆栈超过最大值。
// 3.1 数组长度为负数 [].length = -5 // Uncaught RangeError: Invalid array length // 3.2 Number对象的方法参数超出范围 var num = new Number(12.34) console.log(num.toFixed(-1)) // Uncaught RangeError: toFixed() digits argument must be between 0 and 20 at Number.toFixed // 说明: toFixed方法的作用是将数字四舍五入为指定小数位数的数字,参数是小数点后的位数,范围为0-20. //3.3内存溢出 var num=0; function foo(){ console.log(++num); foo(); } foo(); //Uncaught RangeError: Maximum call stack size exceeded变量或参数不是预期类型时发生的错误。
// 4.1 调用不存在的方法 123() // Uncaught TypeError: 123 is not a function var o = {} o.run() // Uncaught TypeError: o.run is not a function // 4.2 new关键字后接基本类型 var p = new 456 // Uncaught TypeError: 456 is not a constructor // 4.3尝试给undefined和null设置或是访问属性 null.name //Uncaught TypeError: Cannot read property 'name' of null null.age=1 //Uncaught TypeError: Cannot red property 'age' of null undefined.name //Uncaught TypeError: Cannot read property 'name' of undefined undefined.age=1 //Uncaught TypeError: Cannot read property 'name' of undefined // 4.1 调用不存在的方法 123() // Uncaught TypeError: 123 is not a function var o = {} o.run() // Uncaught TypeError: o.run is not a function // 4.2 new关键字后接基本类型 var p = new 456 // Uncaught TypeError: 456 is not a constructor // 4.3尝试给undefined和null设置或是访问属性 null.name //Uncaught TypeError: Cannot read property 'name' of null null.age=1 //Uncaught TypeError: Cannot red property 'age' of null undefined.name //Uncaught TypeError: Cannot read property 'name' of undefined undefined.age=1 //Uncaught TypeError: Cannot read property 'name' of undefined