关于(javascript) "if"关键字的一个疑惑的地方
一直以来对if(obj)的用法有点疑惑,不知道如果obj是一个对象的时候(而不是简单的true/false),该怎么执行。在什么情况下该为true,什么情况下又该为false.
通过下面的小测试,了解到,当obj="",obj=undefined,obj=null,obj=NaN 的时候返回false,obj不为空的时候则返回true。
var obj ;//undefined
if(obj)alert('0:init');
obj = "";//\0
if(!obj)alert('1:\"\"');
obj = undefined;//undefined
if(!obj)alert('2:undefined');
obj = null;
if(!obj)alert("3:null");
obj = false ;
if(!obj)alert("4:false");
obj = true ;
if(!obj)alert("5:true");
obj = "string";
if(!obj)alert("6:string");
obj = NaN;
if(!obj)alert("7:NaN");
执行的结果:
1:""
2:undefined
3:null
4:fasel
7:NaN
当用var 定义一个变量的时候,如果不初始化,则默认值是undefined。
相关文档:
//服务器弹出对话框
if (lcSID == "1" && (txtNsp.Text.Trim() != lcNewspaper || Convert.ToDateTime(txtNspdate.Text.Trim()) != Convert.ToDateTime(lcNspdate)))
{
string lcJs = "v ......
此例子是javascript高级程序设计里的
放出来心备以后使用。上代码。
<html>
<head>
<title>Table Sort Example</title>
<mce:script type="text/javascript"><!--
function convert(sValue, sDataType) {
swi ......
在页面上回车后,不做任何动作。
<script type="text/javascript">
function document.onkeydown()
{
if(event.keyCode ==   ......
经常会在js里用到数组,比如 多个名字相同的input, 若是动态生成的, 提交时就需要判断其是否是数组.
if(document.mylist.length != "undefined" ) {} 这个用法有误.
正确的是 if( typeof(document.mylist.length) != "undefined" ) {}
或 if( !isNaN(document.mylist.length) ) {}
typeof ......