当前位置: 首页 > Web前端 > HTML

问题39:JS数据类型有哪些?

时间:2023-04-02 17:42:14 HTML

数据类型基本类型String,Number,Boolean,Null,Undefined引用类型Object,Array,Function判断数据类型1.使用typeoftypeof'test'//stringtypeof1880//numbertypeoftrue//booleantypeofnull//objecttypeofundefined//undefinedtypeof{}//objecttypeof[]//objecttypeoffunction(){}//function缺点:判断基本类型还是可以的,但是判断引用类型就没那么有用了。无法检查数组、对象、Null2。使用构造函数letxiaoming='text';xiaoming.constructor//String(){}letxiaoming=1880;xiaoming.constructor//Number(){}letxiaoming=true;xiaoming.constructor//Boolean(){}letxiaoming=null;xiaoming.constructor//报错letxiaoming=undefined;xiaoming.constructor//报错letxiaoming={};xiaoming.constructor//Object(){}letxiaoming=[];xiaoming.constructor//数组(){}letxiaoming=function(){};xiaoming.constructor//Function(){}缺点:不能检查Null和Undefined3。使用instanceofffunctionA(){}functionA1(){}functionB(){}A1.prototype=新的();//A1继承Aletxiaoming=newA1();//实例化console.log(xiaominginstanceofA);//trueconsole.log(xiaominginstanceofA1);//trueconsole.log(xiaominginstanceofB);//false缺点:只适用于判断对象属于什么类型,其他不适用4.使用Object.prototype.toString.call()Object.prototype.toString.call('text');//[对象字符串]Object.prototype.toString.call(1880);//[对象编号]Object.prototype.toString.call(true);//[对象布尔]Object.prototype.toString.call(null);//[objectNull]Object.prototype.toString.call(undefined);//[对象未定义]Object.prototype.toString.call({});//[object对象]Object.prototype.toString.call([]);//[对象数组]Object.prototype.toString.call(function(){});//[objectFunction]目前比较准确的一种方式。文章内容/灵感借鉴自下文【持续维护/更新500+前端面试题/笔记】https://github.com/noxussj/In...【大数据可视化图表插件】]https://www.npmjs.com/package...[THREE.JS3D城市建模(珠海市)实现]https://3d.noxussj.top/