JavaScript的数据类型

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 的实例重写了构造方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值