js判断变量类型,类型转换,

1.typeof 操作符 主要检测基础数据类型

var a="zhangqian";  
var b=true;  
var c=10;  
var d;  
var e=null;  
var f=new Object(); 
function add(a,b){ return a+b; }
var tmp = new add(); alert(
typeof a); //string alert(typeof b); //number alert(typeof c); //boolean alert(typeof d); //undefined alert(typeof e); //object alert(typeof f); //object
alert(typeof add ); //function
alert(typeof tmp); // object

alert(null == undefined); //输出 "true"
 

note: javascript的函数属于对象类型,null属于object,null 被认为是对象的占位符,如果typeof的对象是正则表达式,

在Safari和Chrome中使用typeof的时候会返回"function",其他的浏览器返回的是object.

如何避免把函数成obj,从而去遍历一个函数的属性呢?  答案: 现代浏览器基本都会返回成function.

 但是对于装箱后的类型:

比如:

var str = 'asdf';
var str1 = new String('asdf');
typeof str;              //"string"
typeof str1;             //"object"
console.log(str);                   //  'asdf'
console.log(str1);                  // String {0: "a", 1: "s", 2: "d", 3: "f", length: 4, [[PrimitiveValue]]: "asdf"}
console.log( Object.prototype.toString.call(str) ) ;     //"[object String]"
Object.prototype.toString.call(str1);                    //"[object String]"
Object.prototype.toString.call(str) === "[object String]" // 所以最保险的方法是这样判断

 

note: js中的==和 ===,与!= 和!==  ( (等同)双等于会自动转换类型然后匹配, (恒等)三等于不会转换类型,即要求类型和内容完全相等为true)  !=和!==依此类推

var num = 1;
var tmp = '1';
console.log(num == tmp);   // true
console.log(num === tmp);  // false

 

2.instanceof 主要检测引用类型

var array=new Array();  
var object=new Object();  
var regexp=new RegExp();  
function Person(){};  
var p=new Person();  
  
alert(array instanceof Array);    //true  
alert(object instanceof Object);  //true  
alert(regexp instanceof RegExp);  //true  
alert(p instanceof Person);       //true  

alert(array instanceof Object); // true
alert(regexp instanceof Object); // true

note: Object是所有对象的基类,所以在alert(array instanceof Object) 返回的结果同样也是true.

 

js判断null

var exp = null;
if (!exp && typeof exp != "undefined" && exp != 0)
{
    alert("is null");
}

typeof exp != "undefined" 排除了 undefined;
exp != 0 排除了数字零和 false。

更简单的正确的方法:
var exp = null;
if (exp === null)
{
    alert("is null");
}

 

判断数值

isNaN(x)
如果 x 是特殊的非数字值 NaN(或者能被转换为这样的值),返回的值就是 true。如果 x 是其他值,则返回 false。

更具体的判断方法
/^\d+$/  //非负整数(正整数 + 0)
/^[0-9]*[1-9][0-9]*$/  //正整数
/^((-\d+)|(0+))$/  //非正整数(负整数 + 0)
/^-[0-9]*[1-9][0-9]*$/  //负整数
/^-?\d+$/    //整数
/^\d+(\.\d+)?$/  //非负浮点数(正浮点数 + 0)
/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/  //正浮点数
/^((-\d+(\.\d+)?)|(0+(\.0+)?))$/  //非正浮点数(负浮点数 + 0)
/^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/  //负浮点数
/^(-?\d+)(\.\d+)?$/  //浮点数 

var r = /^[0-9]*[1-9][0-9]*$/  //正整数
r.test(str); //str为你要判断的字符 执行返回结果 true 或 false 

 

一些其它方式判断数值(但都不好):

http://www.jb51.net/article/75813.htm

 

 

 

js 区分数组和对象的方法:
var obj = {name:'xxx',pass:'2232'};
var arr = [1,2,3,4];

1.
if(obj instanceof Array){  } // false  
if(arr instanceof Array){  } // true  

2. 通过length属性
obj.length  // undefined
arr.length  // 0 ...  总之大于等于0

3.通过继承的原型对象
Object.prototype.toString.call(obj) === '[object Array]'


note:
不可以通过 typeof(arr) 的方法 ,因为obj和arr都会返回 object



posted @ 2016-03-21 15:48  wifix  阅读(468)  评论(0编辑  收藏  举报