在Javascript中查找解析错误 [英] Finding Parse Errors in Javascript

查看:65
本文介绍了在Javascript中查找解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以在javascript代码中找到解析错误?

Is there an easy way to find parse errors in javascript code?

上周我调试了一个javascript问题,其中第一个调用的javascript函数给出了'对象预期'错误。后来我确定这是因为浏览器无法解析我的javascript代码。我最终解决了这个问题,但这是一个痛苦的过程,包括逐行扫描我的代码,试图找出我的错误。

Last week I was debugging a javascript problem where the first javascript function that was called gave an 'object expected' error. I later determined that this was because the browser wasn't able to parse my javascript code. I eventually solved the problem but it was a painful process that involved pouring over my code line by line, trying to find my mistake.

必须有一个更简单的方法。 / p>

There must be an easier way.

推荐答案

使用 Jslint 或替代浏览器。

直到最近,IE还是唯一没有内置开发帮助的浏览器。其他浏览器将a)在他们遇到的第一个错误中没有停止,并且b)告诉你代码中的问题是什么以及在哪里。

Until recently, IE was the only browser that did not have built in development assistance. Other browsers will a) not come to a grinding halt on the first error they encounter, and b) tell you what and where the problem in your code is.

我的最爱测试IE语法问题的快速简单易用方法是在 Opera 中加载页面。它解析像IE这样的代码,但会给你有意义的错误消息。

My favorite "quick ad easy" way to test IE syntax problems is to load the page up in Opera. It parses code like IE but will give you meaningful error messages.

我将用一个例子说明:

var foo = {
  prop1 : 'value',
  prop2 : 'value',
  prop2 : 'value',   // <-- the problem
};

如果我没记错的话:在IE6和IE7中代码会中断,因为IE要求留下最后一个逗号。解析器抛出一个拟合,浏览器就会停止。它可能会警告某些错误,但行号(甚至文件名)将不可靠。但是,Firefox和Safari只是忽略逗号。 Opera运行代码,但会在控制台上输出错误,指示行号(以及更多)。

If I remember correctly: In IE6 and IE7 the code will break because IE demands leaving the last comma out. The parser throws a fit and the browser simply stops. It may alert some errors but the line numbers (or even filenames) will not be reliable. Firefox and Safari, however, simply ignore the comma. Opera runs the code, but will print an error on the console indicating line number (and more).

编写JavaScript的最简单方法是使用 Firefox + Firebug 。用IE测试并让Opera告诉你它什么时候会破坏。

Easiest way to write JavaScript is with Firefox + Firebug. Test with IE and have Opera tell you what is breaking when it does.

这篇关于在Javascript中查找解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆