if(一个变量),没有> < !=这些半短逻辑, 什么时候为真 什么时候为假?
------解决方案--------------------
仅供参考:
<script type="text/javascript">
var r = "";
window.onload = function () {
var a;
wLine(((a) ? "true " : "false ") + typeof (a)); //false a==undefined
wLine(((a=null) ? "true " : "false ") + typeof (a)); //false a==null
wLine(((a = -0) ? "true " : "false ") + typeof (a)); //false a==0
wLine(((a = -0.000000000001) ? "true " : "false ") + typeof (a)); //true a!=0
wLine(((a = "") ? "true " : "false ") + typeof (a)); //false a==""
wLine(((a = " ") ? "true " : "false ") + typeof (a)); //true a==" " 空格
wLine(((a = function () { return false; }) ? "true " : "false ") + typeof (a)); //true a==function
wLine(((a =( function () { return false; }))() ? "true " : "false ") + typeof (a)); //false a==function
}
function wLine(val) {
var f = document.forms[0];
if(f)f.innerHTML+=val+"<hr/>";
else alert(val);
}
</script>
------解决方案--------------------
javascript是弱变量类型语言,通俗地就是没有数据类型。任意值都有逻辑值(真或假):
非真值:null undefined '' 0 false
真值:任意对象、任意非0数字、任意非空字符串、true
if(a){}表示如果a为真值就执行。