1. 不要使用 alert
首先, alert
只能打印出字符串,如果打印的对象不是 String
,则会调用 toString()
方法将该对象转成字符串(比如转成 [object Object]
这种),所以除非你打印 String
类型的对象,其他什么信息都获取不到。其次, alert
会阻塞UI和javascript的执行,必须点击'OK'按钮才能继续,非常低效。所以,喜欢使用 alert
的同学可以改改这个习惯了。
2. 学会使用 console.log
console.log
谁都会用,但是很多同学只知道最简单的 console.log(x)
这样打印一个对象,当你的代码里面 console.log
多了之后,会很难将某条打印结果和代码对应,所以我们可以给打印信息加上一个标签便于区分:
<pre class="" Liberation Mono", Menlo, Courier, monospace; color: rgb(0, 0, 0); text-align: start;">
-
let x = 1;
-
console.log('aaaaaaaa', x);
</pre>