js 面对对象编程:if 中可以使用那些作为推断条件呢?在所有编程语言中 if 是最长用的推断之一,但在 js 中到底哪些东西可以在 if 中式作为推断表达式呢?例如如何几行,只是少了一个括号,真假就完全不同,到底表示什么含义呢 var obj={}; obj.Funtext=function(){}; if(obj.Funtext) { alert("true obj.Funtext;"); } else { alert("false obj.Funtext"); } obj.Funtext=function(){}; if(obj.Funtext()) { alert("true obj.Funtext();"); } else { alert("false obj.Funtext()"); }1 第一类已定义的变量但未赋值在 if 中认为是假例如: var t; if(t) { alert("true 已定义未赋值"); } else { alert("false 已定义未赋值"); }2 第二类已定义的变量,赋值为空字符串在 if 中认为是假,赋值为其他的字符串,也就是是字符串中有字符就认为是真例如: var t; t=""; if(t) { alert("true t='';"); } else { alert("false t=''"); }if 推断是假再例如: var t; t=" "; if(t) { alert("true t=' ';"); } else { alert("false t=' '"); } t="111"; if(t) { alert("true t='111';"); } else { alert("false t='111'"); }if 推断是真,也就是对于字符串类型,只要有字符,即使是空格字符 if 推断也为真。3 第三类已定义的变量,赋值为 true 在 if 中认为是真,赋值为 false,则为假,这和其他语言中 bool 的类型的变量是一样的。例如: var t; t=false; if(t) { alert("true t=false;"); } else { alert("false t=false;"); } t=true; if(t) { alert("true t=true;"); } else { alert("false t=true;"); }4 第四类已定义的变量,赋值为 0 在 if 中则为假,其他数值认为是真,这和 c 语言中数值的类型的变量是一样的。例如: var t; t=0; if(t) { alert("true t=0;"); } else { alert("false t=0;"); } t=0.0; if(t) { alert("true t=0.0;"); } else { alert("false t=0.0;"); }测试发现不管是 0,还是 0.0 都是假 var t; t=2; if(t) { alert("true t=2;"); } else { alert("false t=2;"); }发现非 0 是都是真5 第五类 js 中的特别值 null,undefined,都是假var t=null; if(t) { alert("true t=null;"); } else { alert("false t=null;"); } t=undefin...