(看到一篇调试JS很有效的文章,珍藏一下)
JavaScript 调试是一场恶梦:起首给出的毛病非常难以明白,其次给出的行号不总有协助。有个查找毛病寄义,及修复步伐的列表,是不是是很有效?
以下是新鲜的 JavaScript 毛病列表。一样的毛病,差别的浏览器会给出差别的音讯,因而有一些差别的例子。
怎样读懂毛病?
起首,让我们疾速看下毛病信息的构造。明白构造有助于明白毛病,假如碰到列表以外的毛病会削减贫苦。
Chrome 中典范的毛病像如许:
Uncaught TypeError: undefined is not a function
毛病的构造以下:
-
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
当尝试挪用一个像要领的值时,这个值并非一个要领。比方:
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 毛病以及怎样修复