javascript
JS常见报错与修复
如何讀懂錯誤?
首先,讓我們快速看下錯誤信息的結(jié)構(gòu)。理解結(jié)構(gòu)有助于理解錯誤,如果遇到列表之外的錯誤會減少麻煩。
Chrome 中典型的錯誤像這樣:
Uncaught TypeError: undefined is not a function錯誤的結(jié)構(gòu)如下:
其它基于 webkit 的瀏覽器,比如 Safari ,給出的錯誤格式跟 Chrome 很類似。Firefox 也類似,但是不總包含第一部分,最新版本的 IE 也給出比 Chrome 簡單的錯誤 - 但是在這里,簡單并不總代表好。
以下是真正的錯誤。
Uncaught TypeError: undefined is not a function
相關(guān)錯誤:
number is not a function, object is not a function, string is not a function, Unhandled Error: ‘foo’ is not a function, Function Expected當(dāng)嘗試調(diào)用一個像方法的值時,這個值并不是一個方法。比如:
var foo = undefined; foo();如果你嘗試調(diào)用一個對象的方法時,你輸錯了名字,這個典型的錯誤很容易發(fā)生。
var x = document.getElementByID('foo');由于對象的屬性不存在,默認(rèn)是?undefined?,以上代碼將導(dǎo)致這個錯誤。嘗試調(diào)用一個像方法的數(shù)字,“number is not a function” 錯誤出現(xiàn)。
如何修復(fù)錯誤:確保方法名正確。這個錯誤的行號將指出正確的位置。
Uncaught ReferenceError: Invalid left-hand side in assignment
相關(guān)錯誤:
Uncaught exception: ReferenceError: Cannot assign to ‘functionCall()’, Uncaught exception: ReferenceError: Cannot assign to ‘this’嘗試給不能賦值的東西賦值,引起這個錯誤。
這個錯誤最常見的例子出現(xiàn)在 if 語句使用:
if(doSomething() = 'somevalue')此例中,程序員意外地使用了單個等號,而不是雙等號。“l(fā)eft-hand side in assignment” 提及了等號左手邊的部分,因此你可以看到以上例子,左手邊包含不能賦值的東西,導(dǎo)致這個錯誤。
如何修復(fù)錯誤:確保沒有給函數(shù)結(jié)果賦值,或者給?this?關(guān)鍵字賦值。
Uncaught TypeError: Converting circular structure to JSON
相關(guān)錯誤:
Uncaught exception: TypeError: JSON.stringify: Not an acyclic Object, TypeError: cyclic object value, Circular reference in value argument not supported把循環(huán)引用的對象,傳給?JSON.stringify?總會引起錯誤。
var a = { }; var b = { a: a }; a.b = b; JSON.stringify(a);由于以上的?a?和?b?循環(huán)引用彼此,結(jié)果對象無法轉(zhuǎn)換成 JSON。
如何修復(fù)錯誤:?移除任何想轉(zhuǎn)換成 JSON 的對象中的循環(huán)引用。
Unexpected token ;
相關(guān)錯誤:
Expected ), missing ) after argument listJavaScript 解釋器預(yù)期的東西沒有被包含。不匹配的圓括號或方括號通常引起這個錯誤,錯誤信息可能有所不同 -?“Unexpected token ]”?或者?“Expected {”?等。
如何修復(fù)錯誤:?有時錯誤出現(xiàn)的行號并不準(zhǔn)確,因此很難修復(fù)。
- [ ]?{ }?( )?這幾個符號不配對常常導(dǎo)致出錯。檢查所有的圓括號和方括號是否配對。行號指出的不僅是問題字符。
- Unexpected /?跟正則表達(dá)式有關(guān)。此時行號通常是正確的。
- Unexpected ;?對象或者數(shù)組字面量里面有個;通常引起這個錯誤,或者函數(shù)調(diào)用的參數(shù)列表里有個分號。此時的行號通常也是正確的。
Uncaught SyntaxError: Unexpected token ILLEGAL
相關(guān)錯誤:
Unterminated String Literal, Invalid Line Terminator一個字符串字面量少了結(jié)尾的引號。
如何修復(fù)錯誤:?確保所有的字符串都有結(jié)束的引號。
Uncaught TypeError: Cannot read property ‘foo’ of null, Uncaught TypeError: Cannot read property ‘foo’ of undefined
相關(guān)錯誤:
TypeError: someVal is null, Unable to get property ‘foo’ of undefined or null reference嘗試讀取?null?或者?undefined?,把它當(dāng)成了對象。例如:
var someVal = null; console.log(someVal.foo);如何修復(fù)錯誤:?通常由于拼寫錯誤導(dǎo)致。檢查錯誤指出的行號附近使用的變量名是否正確。
Uncaught TypeError: Cannot set property ‘foo’ of null, Uncaught TypeError: Cannot set property ‘foo’ of undefined
相關(guān)錯誤:
TypeError: someVal is undefined, Unable to set property ‘foo’ of undefined or null reference嘗試寫入?null?或者?undefined?,把它當(dāng)成了一個對象。例如:
var someVal = null; someVal.foo = 1;如何修復(fù)錯誤:?也是由于拼寫錯誤所致。檢查錯誤指出的行號附近的變量名。
Uncaught RangeError: Maximum call stack size exceeded
相關(guān)錯誤:
Related errors: Uncaught exception: RangeError: Maximum recursion depth exceeded, too much recursion, Stack overflow通常由程序邏輯 bug 引起,導(dǎo)致函數(shù)的無限遞歸調(diào)用。
如何修復(fù)錯誤:?檢查遞歸函數(shù)中可能導(dǎo)致無限循環(huán) 的 bug 。
Uncaught URIError: URI malformed
相關(guān)錯誤:
URIError: malformed URI sequence無效的 decodeURIComponent 調(diào)用所致。
如何修復(fù)錯誤:?按照錯誤指出的行號,檢查?decodeURIComponent?調(diào)用,它是正確的。
XMLHttpRequest cannot load [http://some/url/](http://some/url/). No ‘Access-Control-Allow-Origin’ header is present on the requested resource
相關(guān)錯誤:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [http://some/url/](http://some/url/)錯誤肯定是使用 XMLHttpRequest 引起的。
如何修復(fù):?確保請求的 URL 是正確的,它遵循同源策略?。最好的方法是從代碼中找到錯誤信息指出的 URL 。
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
相關(guān)錯誤:
InvalidStateError, DOMException code 11代碼調(diào)用的方法在當(dāng)前狀態(tài)無法調(diào)用。通常由?XMLHttpRequest?引起,在方法準(zhǔn)備完畢之前調(diào)用它會引起錯誤。
var xhr = new XMLHttpRequest(); xhr.setRequestHeader('Some-Header', 'val');這時就會出錯,因為?setRequestHeader?方法只能在?xhr.open?方法之后調(diào)用。
如何修復(fù):?查看錯誤指出的行號,確保代碼運行的時機正確,或者在它(例如?xhr.open)之前添加了不必要的調(diào)用
結(jié)論
我看過不少無用的 JavaScript 錯誤,比如 PHP 中聲名狼藉的異常?Expected T_PAAMAYIM_NEKUDOTAYIM?。拋出更熟悉的錯誤才更有意義。現(xiàn)代瀏覽器不再拋出完全無用的錯誤,才會更有幫助。
轉(zhuǎn)載于:https://www.cnblogs.com/fubi/p/6929818.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
- 上一篇: 设计模式笔记3:设计模式几大原则
- 下一篇: JS常用属性方法大全