

Debugging JavaScript:throw与console
source link: https://blogread.cn/it/article/1313?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Debugging JavaScript:throw与console
我想没多少人敢保证写JavaScript能不用调试,那选择用什么方式调试会比较好呢?
- 告别了我最爱的
alert("MM")
- 告别了我最爱的
document.title="MM"
- 告别了我最爱的
window.status="MM"
大家开始用各种新的方式来看调试信息。。。比如最近sitepoint就发了一篇Debugging JavaScript: Throw Away Your Alerts!,大家先去看,看完再回来接着看。
var err = new Error(); err.name = 'My API Input Error'; err.message = 'Input must be a number'; throw(err);
throw
虽然不错,但是我们调试时可能不只能抛错,还可能抛很多奇怪的信息,比如把执行时间、返回结果打出来看,当然,你也可以写不同的err.name
来区分。但是,有事没事看到报错也是很不爽的一件事。
写JavaScript的同学应该都有装Firebug,如果没装赶快装去,装完了再回来看,要是连Firefox都没有就玩Firebug Lite。
Firebug提供有了个console,你可以用:console.log(object[, object, ...])
和若干console.xxx
来玩,这样就可以把错误跟警告区分开了,挖哈哈。
同时,console并不是Firebug独有的,你也可以用console.log('嗷嗷又出来忽悠人了!');
把信息打到Safari的错误控制台,还可以这样玩:
console.warn('这个图标很黄');
console.error('×的图标不好看');
Opera也有个错误控制台,可惜不能用console.log
来显示东东,不过也有打印的接口: opera.postError
,但它打出的的图标却不是错误的图标,它的真名应该叫postWarn,throw
打出来的图标才是错误的图标,它才是真正的postError,算了,凑和着用吧。如果在Opera下做开发推荐使用Opera Dragonfly。
还有IE呢?嗯,介于我的系统没有IE,飘过。挖哈哈。
其实大家可以自己写个Console,除了可以把东东打印到浏览器的错误控制台上,还可以把信息打印到服务器上,在自己调试环境下没问题不代表在任何情况下都会没问题。
Recommend
-
110
I must admit it for the first time, and I’ll use this platform to clean up the skeletons from my development closet. Sometimes, the magic I do — which some call “coding” — is not as perfect as the…
-
38
Moving beyond console.log() — 8 Console Methods You Should Use When Debugging JavaScript and NodeMoving beyond console.log and learn the console functions you have never used for debugging!
-
16
more universal debugging with consoleotsukare Thoughts after a day of workmore universal debugging with console
-
12
-
4
Recently, I was involved in a discussion about error handling. We couldn't agree on proper usage of console.error and when to use throw. So I've decided to write a short post on how...
-
7
程序在运行中难免遇到 bug,所以就需要好的调试手段找出问题所在,try, catch, throw 便是 JavaScript 中用来调试并对错误执行相关操作的工具,下面具体介绍其用法; try, catch 基本语法结构: try {...
-
11
9 JavaScript Console Tips That Will Improve Your Debugging SkillsThere’s more to using console.log() to debug code. Learn more about the different JS console methods available.One of the easiest ways to...
-
10
The console is a JavaScript object that grants developers access to a browsers debugging console. Shortcuts to open the console in browsers Ctrl + Shift + I (windows) Command + Option + K (Mac) ...
-
9
11 Console Methods in JavaScript for Effective DebuggingJavaScript console methods allow develo...
-
11
Blog / JavaScript / Error Handling in JavaScript: A Guide to Try/Catch, Throw, and Custom Error Classes Erro...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK