JS常見報錯與修複

來源:互聯網
上載者:User

標籤:新版本   imu   導致   read   tin   結果   exception   ack   方法   

如何讀懂錯誤?

首先,讓我們快速看下錯誤資訊的結構。理解結構有助於理解錯誤,如果遇到列表之外的錯誤會減少麻煩。

Chrome 中典型的錯誤像這樣:

Uncaught TypeError: undefined is not a function

錯誤的結構如下:

  1. Uncaught TypeError: 這部分資訊通常不是很有用。Uncaught 表示錯誤沒有被 catch 語句捕獲,TypeError 是錯誤的名字。
  2. undefined is not a function: 這部分資訊,你必須逐字閱讀。比如這裡表示代碼嘗試使用 undefined,把它當做一個函數。

其它基於 webkit 的瀏覽器,比如 Safari ,給出的錯誤格式跟 Chrome 很類似。Firefox 也類似,但是不總包含第一部分,最新版本的 IE 也給出比 Chrome 簡單的錯誤 - 但是在這裡,簡單並不總代表好。

以下是真正的錯誤。

Uncaught TypeError: undefined is not a function

相關錯誤:

number is not a function, object is not a function, string is not a function, Unhandled Error: ‘foo’ is not a function, Function Expected

當嘗試調用一個像方法的值時,這個值並不是一個方法。比如:

var foo = undefined;foo();

如果你嘗試調用一個對象的方法時,你輸錯了名字,這個典型的錯誤很容易發生。

var x = document.getElementByID(‘foo‘);

由於對象的屬性不存在,預設是 undefined ,以上代碼將導致這個錯誤。嘗試調用一個像方法的數字,“number is not a function” 錯誤出現。

如何修複錯誤:確保方法名正確。這個錯誤的行號將指出正確的位置。

Uncaught ReferenceError: Invalid left-hand side in assignment

相關錯誤:

Uncaught exception: ReferenceError: Cannot assign to ‘functionCall()’, Uncaught exception: ReferenceError: Cannot assign to ‘this’

嘗試給不能賦值的東西賦值,引起這個錯誤。

這個錯誤最常見的例子出現在 if 語句使用:

if(doSomething() = ‘somevalue‘)

此例中,程式員意外地使用了單個等號,而不是雙等號。“left-hand side in assignment” 提及了等號左手邊的部分,因此你可以看到以上例子,左手邊包含不能賦值的東西,導致這個錯誤。

如何修複錯誤:確保沒有給函數結果賦值,或者給 this 關鍵字賦值。

Uncaught TypeError: Converting circular structure to JSON

相關錯誤:

Uncaught exception: TypeError: JSON.stringify: Not an acyclic Object, TypeError: cyclic object value, Circular reference in value argument not supported

把循環參考的對象,傳給 JSON.stringify 總會引起錯誤。

var a = { };var b = { a: a };a.b = b;JSON.stringify(a);

由於以上的 a 和 b 循環參考彼此,結果對象無法轉換成 JSON。

如何修複錯誤: 移除任何想轉換成 JSON 的對象中的循環參考。

Unexpected token ;

相關錯誤:

Expected ), missing ) after argument list

JavaScript 解譯器預期的東西沒有被包含。不匹配的圓括弧或方括弧通常引起這個錯誤,錯誤資訊可能有所不同 - “Unexpected token ]” 或者 “Expected {” 等。

如何修複錯誤: 有時錯誤出現的行號並不準確,因此很難修複。

  • [ ] { } ( ) 這幾個符號不配對常常導致出錯。檢查所有的圓括弧和方括弧是否配對。行號指出的不僅是問題字元。
  • Unexpected / 跟Regex有關。此時行號通常是正確的。
  • Unexpected ; 對象或者數組字面量裡面有個;通常引起這個錯誤,或者函數調用的參數列表裡有個分號。此時的行號通常也是正確的。

Uncaught SyntaxError: Unexpected token ILLEGAL

相關錯誤:

Unterminated String Literal, Invalid Line Terminator

一個字串字面量少了結尾的引號。

如何修複錯誤: 確保所有的字串都有結束的引號。

Uncaught TypeError: Cannot read property ‘foo’ of null, Uncaught TypeError: Cannot read property ‘foo’ of undefined

相關錯誤:

TypeError: someVal is null, Unable to get property ‘foo’ of undefined or null reference

嘗試讀取 null 或者 undefined ,把它當成了對象。例如:

var someVal = null;console.log(someVal.foo);

如何修複錯誤: 通常由於拼字錯誤導致。檢查錯誤指出的行號附近使用的變數名是否正確。

Uncaught TypeError: Cannot set property ‘foo’ of null, Uncaught TypeError: Cannot set property ‘foo’ of undefined

相關錯誤:

TypeError: someVal is undefined, Unable to set property ‘foo’ of undefined or null reference

嘗試寫入 null 或者 undefined ,把它當成了一個對象。例如:

var someVal = null;someVal.foo = 1;

如何修複錯誤: 也是由於拼字錯誤所致。檢查錯誤指出的行號附近的變數名。

Uncaught RangeError: Maximum call stack size exceeded

相關錯誤:

Related errors: Uncaught exception: RangeError: Maximum recursion depth exceeded, too much recursion, Stack overflow

通常由程式邏輯 bug 引起,導致函數的無限遞迴調用。

如何修複錯誤: 檢查遞迴函式中可能導致無限迴圈 的 bug 。

Uncaught URIError: URI malformed

相關錯誤:

URIError: malformed URI sequence

無效的 decodeURIComponent 調用所致。

如何修複錯誤: 按照錯誤指出的行號,檢查 decodeURIComponent 調用,它是正確的。

XMLHttpRequest cannot load [http://some/url/](http://some/url/). No ‘Access-Control-Allow-Origin’ header is present on the requested resource

相關錯誤:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at[http://some/url/](http://some/url/)

錯誤肯定是使用 XMLHttpRequest 引起的。

如何修複: 確保請求的 URL 是正確的,它遵循同源策略 。最好的方法是從代碼中找到錯誤資訊指出的 URL 。

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

相關錯誤:

InvalidStateError, DOMException code 11

代碼調用的方法在目前狀態無法調用。通常由 XMLHttpRequest 引起,在方法準備完畢之前調用它會引起錯誤。

var xhr = new XMLHttpRequest();xhr.setRequestHeader(‘Some-Header‘, ‘val‘);

這時就會出錯,因為 setRequestHeader 方法只能在 xhr.open 方法之後調用。

如何修複: 查看錯誤指出的行號,確保代碼啟動並執行時機正確,或者在它(例如 xhr.open)之前添加了不必要的調用

結論

我看過不少無用的 JavaScript 錯誤,比如 PHP 中聲名狼藉的異常 Expected T_PAAMAYIM_NEKUDOTAYIM 。拋出更熟悉的錯誤才更有意義。現代瀏覽器不再拋出完全無用的錯誤,才會更有協助。

JS常見報錯與修複

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.