Page 97 - JavaScript修炼之道
P. 97
附录A JavaScript快速参考 89
timer && window.clearTimeout(timer);
timer = null;
回调代码;
}
window.setTimeout(callback, 100); //0.1秒
可怜人的调试器
①
全局的window对象含有alert(...)方法,调用它可以弹出一个对话框。如果可能的话(使
用Safari、装有Firebug的Firefox以及Opera、IE8+),使用控制台机制来获得更轻松的调试体验。
Firebug的console对象在console.log的基础上增加了大量的内容。为了获得更多信息,请
登录 http://getfirebug.com/logging。
循环
for(decl; test; incr){ while(cond) {
代码 可能永远不会执行的代码
} }
do { while(true) {
至少会执行一次的代码 至少会执行一次的代码
} while(条件); if(跳过接下来的内容)
continue;
if(停止当前循环)
break;
其余的代码
“快速数组循环”
for(var i=0, l=arr.length; i<l; ++i)
代码
非常有用的字符串操作
这里并没有提到length属性(返回包含的字符个数,而非字节的个数)。
'yay'.charAt(1) // => 'a'
'hello'.indexOf('l') // => 2
'hello'.indexOf('l', 3) // => 3(指定了搜索起始位置以最小返回值)
'hello'.lastIndexOf('l') // => 3
'hello'.lastIndexOf('l', 2) // => 2(指定了搜索结束位置以最大返回值)
'hello'.replace('l', 'L') // => 'heLlo'
'hello'.replace(/[aeiouy]/g, '-') // => 'h-ll-'
'hello'.replace(/(.)\1/g, function(s){
——————————
① 本书作者认为使用alert(...)进行JavaScript调试过于原始,阅读附录B以获得关于JavaScript调试的更多细节。*