(看到一篇调试JS颇有用的文章,收藏一下)javascript
JavaScript 调试是一场噩梦:首先给出的错误很是难以理解,其次给出的行号不总有帮助。有个查找错误含义,及修复措施的列表,是否是颇有用?java
如下是奇怪的 JavaScript 错误列表。一样的错误,不一样的浏览器会给出不一样的消息,所以有一些不一样的例子。
git
首先,让咱们快速看下错误信息的结构。理解结构有助于理解错误,若是遇到列表以外的错误会减小麻烦。程序员
Chrome 中典型的错误像这样:github
Uncaught TypeError: undefined is not a function
错误的结构以下:web
Uncaught TypeError:
这部分信息一般不是颇有用。Uncaught
表示错误没有被 catch
语句捕获,TypeError
是错误的名字。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
当尝试调用一个像方法的值时,这个值并非一个方法。好比:ide
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 /
跟正则表达式有关。此时行号一般是正确的。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
。抛出更熟悉的错误才更有意义。现代浏览器再也不抛出彻底无用的错误,才会更有帮助。
原文Jani Hartikainen - 《JavaScript Errors and How to Fix Them》
翻译出处:涂鸦码农 - JavaScript 错误以及如何修复