六种数据类型:
1.number
2.string
3.boolean
4.null
5.undefined
6.object
检查数据类型的办法:
1.typeof xxx
var a = 123;
typeof a ; //number
var a = "123";
typeof a; //string
var a = false;
typeof a; //boolean
var a = null;
typeof a; //object (null是空对象引用或者说指针)
var a;
typeof a; //undefined
var a = document.body;
typeof a ; //object
2.instanceof
instanceof 是判断对象是已知数据类型的办法
var a = [] //创建一个new Array
a instanceof Array //true
var a = function(){}
a instanceof Function //true
注意:instanceof 后面一定是数据类型,并且大小写不能错,该方法适合一些条件或分支
3.constructor
var a = function(){};
a.constructor === Function //true
constructor不能类继承
function a(){};
function b(){};
a.prototype = new b();//继承b属性
var c = new a();
console.log(a); //查看属性
console.log(c);
c.constructor === a; //false
解决办法:constructor指向其本身
function a(){};
function b(){};
a.prototype = new b();//继承b属性
var c = new a();
console.log(a.constructor); //ƒ Function() { [native code] }
console.log(c.constructor); //ƒ b(){}
c.constructor = a; //将自己的类赋值给对象的constructor属性
c.constructor === a; //true
4.原型(prototype)
var a = []; //new Array()
Object.prototype.toString.call(a) === '[object Array]' //true
var a = ""; //new string()
Object.prototype.toString.call(a) === '[object String]' //true
var a = 123; //new number()
Object.prototype.toString.call(a) === '[object Number]' //true
通用性比较好