判断数据类型

js判断数据类型

常用的为4种方法(typeof,instanceof、constructor、toString),以下介绍的是最为常用的2种方法(typeof、toString),可按需使用。

待测试的数据

var und = undefined;
var nul = null;
var boo = true;
var num = 1;
var str = 'xys'
var obj = new Object();
var arr = [1,2,3];
var fun = function(){}
var date = new Date();
var reg = /a/g;
var err = new Error()
var arg;
(function getArg(){
    arg=arguments;
})();

typeof

console.log(typeof und);  // undefined
console.log(typeof nul);  // object
console.log(typeof boo);  // boolean
console.log(typeof num);  // number
console.log(typeof str);  // string
console.log(typeof obj);  // object
console.log(typeof arr);  // object
console.log(typeof fun);  // function
console.log(typeof date); // object
console.log(typeof reg);  // object
console.log(typeof err);  // object
console.log(typeof arg);  // object

toString

var toString = Object.prototype.toString;
console.log(toString.call(und));  // [object Undefined]
console.log(toString.call(nul));  // [object Null]
console.log(toString.call(boo));  // [object Boolean]
console.log(toString.call(num));  // [object Number]
console.log(toString.call(str));  // [object String]
console.log(toString.call(obj));  // [object Object]
console.log(toString.call(arr));  // [object Array]
console.log(toString.call(fun));  // [object Function]
console.log(toString.call(date)); // [object Date]
console.log(toString.call(reg));  // [object RegExp]
console.log(toString.call(err));  // [object Error]
console.log(toString.call(arg));  // [object Arguments]

摘自https://blog.csdn.net/wzwzwz555/article/details/82865910

 228 total views,  4 views today

Revisions

Comments are closed.