javascript值类型undefined、string、boolean、number、object的判断

<script language="javascript" type="text/javascript">
//去左空格;
function ltrim(s){
 return s.replace( /^(\s*| *)/, "");
}
//去右空格;
function rtrim(s){
 return s.replace( /(\s*| *)$/, "");
}
//去左右空格;
function trim(s){
 return ltrim(rtrim(s));
}
/**
 * 判断变量是否空值
 * undefined, null, '', false, 0, [], {} 均返回true,否则返回false
 */
function empty(v){
    switch (typeof v){
        case 'undefined' : return "undefined";break;
        case 'string'    : return "string";break;
        case 'boolean'   : return "boolean"; break;
        case 'number'    : return "num"; break;
        case 'object'    :
            return "object";
            if(null === v) return true;
            if(undefined !== v.length && v.length==0) return true;
            for(var k in v){return false;} return true;
            break;
    }
    return false;
}

var mytext=new Array();
mytext[0]=2;
mytext[1]="oooo";
mytext[2]={"a":0,"b":2};

for(var i=0;i<mytext.length+2;i++){
    alert(empty(mytext[i]));
}

</script>
posted @ 2011-11-15 01:24  飞虎cnblog  阅读(401)  评论(0编辑  收藏  举报
友情链接:技术迷 | JSM官方博客 | 阿旭博客 | 有声小说在线听