js里面的数据类型包括两种,基本数据类型和引用数据类型
基本数据类型包括:string,number,boolean,bigint,symbol,null,undefined
引用数据类型包括:object,function,array,data,regexp
其中基本数据集类型存放在栈内存中。引用数据类型存放在堆内存中
对于数据类型的判断方式有typeof;写法如下
typeof 2 //number
typeof 'hhh' //string
typeof {} //object
typeof true //boolean
typeof null //object
typeof undefined //undefined
typeof function(){} //object
typeof [] //object
typeof Array //function
instanceof也可用于判断类型是否存在于实例的原型链上返回的是布尔值
[] instanceof Array //true
function(){} instanceof function //true
{} instanceof object //true
constructor用于判断数据的类型,对象实例通过constructor对象访问他的构造函数(如果创建了一个对象改变了原型,就无法判断)
console.log((2).constructor === Number); // true
console.log((true).constructor === Boolean); // true
console.log(('str').constructor === String); // true
console.log(([]).constructor === Array); // true
console.log((function() {}).constructor === Function); // true
console.log(({}).constructor === Object); // true
function Fn(){};
Fn.prototype = new Array();
var f = new Fn();
console.log(f.constructor===Fn); // false
console.log(f.constructor===Array); // true
Object.prototype.toString.call()使用object对象的原型方法tostring判断数据类型
var a = Object.prototype.toString;
console.log(a.call(2));
console.log(a.call(true));
console.log(a.call('str'));
console.log(a.call([]));
console.log(a.call(function(){}));
console.log(a.call({}));
console.log(a.call(undefined));
console.log(a.call(null));
//[object Number]
typeof.html:28 [object Boolean]
typeof.html:29 [object String]
typeof.html:30 [object Array]
typeof.html:31 [object Function]
typeof.html:32 [object Object]
typeof.html:33 [object Undefined]
typeof.html:34 [object Null]
typeof.html:64 Live reload enabled.
Object.prototype.toString.call()和Object.toString存在区别
array和function都是作为object 的实例重写了构造方法