JavaScript 数据类型
1.基本数据类型检测
var str = "test";
var num = 1;
var bool = true;
var undef = undefined;
var nul = null;
var obj = {
a:1
}
var f = function(){
console.log("test");
}
console.log(typeof str); //"string"
console.log(typeof num); //"number"
console.log(typeof bool); //"boolean"
console.log(typeof undef); //"undefined"
console.log(typeof nul); //"object"
console.log(typeof obj); //"object"
console.log(typeof f); //"function"
2.引用类型的数据检测
var arr = [];
var date = new Date();
var reg = /[0-9]/;
var f = function(){
console.log("test");
}
var PackStr = new String("test");
var PackNum = new Number(10);
var PackBool = new Boolean(true);
(1) instanceof 检测
console.log(arr instanceof Array); //true
console.log(date instanceof Date); //true
console.log(reg instanceof RegExp); //true
console.log(f instanceof Object); //true
console.log(PackStr instanceof String); //true
console.log(PackNum instanceof Number); //true
console.log(PackBool instanceof Boolean); //true
(2) 构造函数检测
console.log(arr.constructor == Array); //true
console.log(date.constructor == Date); //true
console.log(reg.constructor == RegExp); //true
console.log(f.constructor == Function); //true
console.log(PackStr.constructor == String); //true
console.log(PackNum.constructor == Number); //true
console.log(PackBool.constructor == Boolean); //true
(3) 原型检测
console.log(arr.__proto__ == Array.prototype); //true
console.log(date.__proto__ == Date.prototype); //true
console.log(reg.__proto__ == RegExp.prototype); //true
console.log(f.__proto__ == Function.prototype); //true
console.log(PackStr.__proto__ == String.prototype); //true
console.log(PackNum.__proto__ == Number.prototype); //true
console.log(PackBool.__proto__ == Boolean.prototype); //true
console.log(Object.getPrototypeOf(arr) == Array.prototype); //true
console.log(Object.getPrototypeOf(date) == Date.prototype); //true
console.log(Object.getPrototypeOf(reg) == RegExp.prototype); //true
console.log(Object.getPrototypeOf(f) == Function.prototype); //true
console.log(Object.getPrototypeOf(PackStr) == String.prototype); //true
console.log(Object.getPrototypeOf(PackNum) == Number.prototype); //true
console.log(Object.getPrototypeOf(PackBool) == Boolean.prototype); //true
console.log(Array.prototype.isPrototypeOf(arr)); //true
console.log(Date.prototype.isPrototypeOf(date)); //true
console.log(RegExp.prototype.isPrototypeOf(reg)); //true
console.log(Function.prototype.isPrototypeOf(f)); //true
console.log(String.prototype.isPrototypeOf(PackStr)); //true
console.log(Number.prototype.isPrototypeOf(PackNum)); //true
console.log(Boolean.prototype.isPrototypeOf(PackBool)); //true
(4) Object.prototype.toString 检测
console.log(Object.prototype.toString.call(arr) == "[object Array]"); //true
console.log(Object.prototype.toString.call(date) == "[object Date]"); //true
console.log(Object.prototype.toString.call(reg) == "[object RegExp]"); //true
console.log(Object.prototype.toString.call(f) == "[object Function]"); //true
console.log(Object.prototype.toString.call(PackStr) =="[object String]"); //true
console.log(Object.prototype.toString.call(PackNum) =="[object Number]"); //true
console.log(Object.prototype.toString.call(PackBool) =="[object Boolean]"); //true