javaScript中常见的几种报错类型
生活随笔
收集整理的這篇文章主要介紹了
javaScript中常见的几种报错类型
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一般我們運(yùn)行代碼的時(shí)候,在控制臺(tái)報(bào)錯(cuò)會(huì)相應(yīng)的顯示你錯(cuò)誤的行數(shù),找到那一行,查找你相應(yīng)的錯(cuò)誤 1、xxx is not defined xxx 沒(méi)有定義 ? 2、xxx is not a function xxx 不是一個(gè)函數(shù) xxx此時(shí)是undefined 3、Cannot read property 'xxx' of undefined 不能讀取undefined的xxx屬性 xxx前面的變量是undefined 4、Cannot set property 'xxx' of null 不能給null設(shè)置xxx屬性 xxx前面的變量是null 5、Invalid or unexpected token 標(biāo)點(diǎn)符號(hào)可能是中文 6、Unexpected token a in JSON at position 0 使用JSON.parse進(jìn)行解析json字符串時(shí),解析的內(nèi)容不合法 7、XMLHttpRequest cannot load http://XXXXXX. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://XXXXXX' is therefore not allowed access. 使用ajax請(qǐng)求數(shù)據(jù)時(shí),產(chǎn)生跨域了 8、Illegal break statement 出現(xiàn)了非法語(yǔ)句 9、SyntaxError(語(yǔ)法錯(cuò)誤) SyntaxError是解析代碼時(shí)發(fā)生的語(yǔ)法錯(cuò)誤 // 變量名錯(cuò)誤 var 1a;? // 缺少括號(hào) console.log ('hello'; 10、ReferenceErro(引用錯(cuò)誤) ReferenceError是引用一個(gè)不存在的變量時(shí)發(fā)生的錯(cuò)誤。 unknownVariable
// ReferenceError: unknownVariable is not defined 另一種觸發(fā)場(chǎng)景是,將一個(gè)值分配給無(wú)法分配的對(duì)象,比如對(duì)函數(shù)的運(yùn)行結(jié)果或者this賦值。 console.log() = 1
// ReferenceError: Invalid left-hand side in assignment this = 1
// ReferenceError: Invalid left-hand side in assignment 上面代碼對(duì)函數(shù)console.log的運(yùn)行結(jié)果和this賦值,結(jié)果都引發(fā)了ReferenceError錯(cuò)誤 11、RangeError(范圍錯(cuò)誤) RangeError是當(dāng)一個(gè)值超出有效范圍時(shí)發(fā)生的錯(cuò)誤。主要有幾種情況,一是數(shù)組長(zhǎng)度為負(fù)數(shù),二是Number對(duì)象的方法參數(shù)超出范圍,以及函數(shù)堆棧超過(guò)最大值。 new Array(-1)
// RangeError: Invalid array length
(1234).toExponential(21)
// RangeError: toExponential() argument must be between 0 and 20 12、TypeError(類型錯(cuò)誤) TypeError是變量或參數(shù)不是預(yù)期類型時(shí)發(fā)生的錯(cuò)誤。比如,對(duì)字符串、布爾值、數(shù)值等原始類型的值使用new命令,就會(huì)拋出這種錯(cuò)誤,因?yàn)閚ew命令的參數(shù)應(yīng)該是一個(gè)構(gòu)造函數(shù)。 new 123
//TypeError: number is not a func var obj = {}; obj.unknownMethod()
// TypeError: undefined is not a function 上面代碼的第二種情況,調(diào)用對(duì)象不存在的方法,會(huì)拋出TypeError錯(cuò)誤。 13、URIError(URI錯(cuò)誤) URIError是URI相關(guān)函數(shù)的參數(shù)不正確時(shí)拋出的錯(cuò)誤,主要涉及encodeURI()、decodeURI()、encodeURIComponent()、decodeURIComponent()、escape()和unescape()這六個(gè)函數(shù)。 decodeURI('%2') // URIError: URI malformed 14、EvalError(eval錯(cuò)誤) eval函數(shù)沒(méi)有被正確執(zhí)行時(shí),會(huì)拋出EvalError錯(cuò)誤。該錯(cuò)誤類型已經(jīng)不再在ES5中出現(xiàn)了,只是為了保證與以前代碼兼容,才繼續(xù)保留。 以上這6種派生錯(cuò)誤,連同原始的Error對(duì)象,都是構(gòu)造函數(shù)。開(kāi)發(fā)者可以使用它們,人為生成錯(cuò)誤對(duì)象的實(shí)例。 new Error("出錯(cuò)了!"); new RangeError("出錯(cuò)了,變量超出有效范圍!"); new TypeError("出錯(cuò)了,變量類型無(wú)效!"); 上面代碼表示新建錯(cuò)誤對(duì)象的實(shí)例,實(shí)質(zhì)就是手動(dòng)拋出錯(cuò)誤。可以看到,錯(cuò)誤對(duì)象的構(gòu)造函數(shù)接受一個(gè)參數(shù),代表錯(cuò)誤提示信息(message)。 15、錯(cuò)誤舉例
// ReferenceError: unknownVariable is not defined 另一種觸發(fā)場(chǎng)景是,將一個(gè)值分配給無(wú)法分配的對(duì)象,比如對(duì)函數(shù)的運(yùn)行結(jié)果或者this賦值。 console.log() = 1
// ReferenceError: Invalid left-hand side in assignment this = 1
// ReferenceError: Invalid left-hand side in assignment 上面代碼對(duì)函數(shù)console.log的運(yùn)行結(jié)果和this賦值,結(jié)果都引發(fā)了ReferenceError錯(cuò)誤 11、RangeError(范圍錯(cuò)誤) RangeError是當(dāng)一個(gè)值超出有效范圍時(shí)發(fā)生的錯(cuò)誤。主要有幾種情況,一是數(shù)組長(zhǎng)度為負(fù)數(shù),二是Number對(duì)象的方法參數(shù)超出范圍,以及函數(shù)堆棧超過(guò)最大值。 new Array(-1)
// RangeError: Invalid array length
(1234).toExponential(21)
// RangeError: toExponential() argument must be between 0 and 20 12、TypeError(類型錯(cuò)誤) TypeError是變量或參數(shù)不是預(yù)期類型時(shí)發(fā)生的錯(cuò)誤。比如,對(duì)字符串、布爾值、數(shù)值等原始類型的值使用new命令,就會(huì)拋出這種錯(cuò)誤,因?yàn)閚ew命令的參數(shù)應(yīng)該是一個(gè)構(gòu)造函數(shù)。 new 123
//TypeError: number is not a func var obj = {}; obj.unknownMethod()
// TypeError: undefined is not a function 上面代碼的第二種情況,調(diào)用對(duì)象不存在的方法,會(huì)拋出TypeError錯(cuò)誤。 13、URIError(URI錯(cuò)誤) URIError是URI相關(guān)函數(shù)的參數(shù)不正確時(shí)拋出的錯(cuò)誤,主要涉及encodeURI()、decodeURI()、encodeURIComponent()、decodeURIComponent()、escape()和unescape()這六個(gè)函數(shù)。 decodeURI('%2') // URIError: URI malformed 14、EvalError(eval錯(cuò)誤) eval函數(shù)沒(méi)有被正確執(zhí)行時(shí),會(huì)拋出EvalError錯(cuò)誤。該錯(cuò)誤類型已經(jīng)不再在ES5中出現(xiàn)了,只是為了保證與以前代碼兼容,才繼續(xù)保留。 以上這6種派生錯(cuò)誤,連同原始的Error對(duì)象,都是構(gòu)造函數(shù)。開(kāi)發(fā)者可以使用它們,人為生成錯(cuò)誤對(duì)象的實(shí)例。 new Error("出錯(cuò)了!"); new RangeError("出錯(cuò)了,變量超出有效范圍!"); new TypeError("出錯(cuò)了,變量類型無(wú)效!"); 上面代碼表示新建錯(cuò)誤對(duì)象的實(shí)例,實(shí)質(zhì)就是手動(dòng)拋出錯(cuò)誤。可以看到,錯(cuò)誤對(duì)象的構(gòu)造函數(shù)接受一個(gè)參數(shù),代表錯(cuò)誤提示信息(message)。 15、錯(cuò)誤舉例
- 找不到引入的.js文件
- js函數(shù)缺少括號(hào)),函數(shù)體缺少大括號(hào)}
- css代碼使用了//注釋,導(dǎo)致有的div內(nèi)容出不來(lái),然后也不報(bào)錯(cuò)
- 定義了多個(gè)同名的變量
- 文檔未加載完畢,就開(kāi)始執(zhí)行js代碼導(dǎo)致的錯(cuò)誤
- js 對(duì)象的函數(shù)未定義
轉(zhuǎn)載于:https://www.cnblogs.com/fengch/p/8656648.html
總結(jié)
以上是生活随笔為你收集整理的javaScript中常见的几种报错类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于YOLO算法的备忘
- 下一篇: 小带鱼上征信吗