ZhangYang's Blog

JavaScript assert

JS assert

自学常用方法,assert来猜自己到底对不对,我错在哪

1
2
3
4
5
6
7
8
9
10
function assert(value, description){
description = description || ''
if(value === true){
console.info('[测试通过]'+ description)
}else{
console.error('[测试不通过]' + description)
}
}
assert('1\n2'.length===3, '\\n占一个字符')

用 assert 学 JS

1
2
3
4
5
6
7
8
9
10
11
12
13
function f1(){
return 'hello world'
}
assert(f1 === 'hello world')
assert(f1() === 'hello world')
function f2(){
return function(){}
}
assert(typeof f1() === 'function')
assert(typeof document === 'object')

学会调试(console.log、console.assert、断点)

先做再理解(Demo 先行)