JS数据类型判断的方法
最常用的判断方法:typeof
var a='isString';
var b=121221;
var c=[1,2,3];
var d=new Date();
var e=function(){
console.log('12');
};
var f=function(){
this.name='22';
};
var g=null;
var h=undefined;
var i=true;
console.log(typeof b) =======> number
console.log(typeof a) =======> string
console.log(typeof h) =======> undefined
console.log(typeof i) =======> boolean
console.log(typeof c) =======> object
console.log(typeof d) =======> object
console.log(typeof g) =======> object
console.log(typeof e) =======> function console.log(typeof f) =======> function
总结 在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。
对于数组、函数、对象来说,其关系错综复杂,使用 typeof 都会统一返回 “object” 字符串。
判断已知对象类型的方法 instanceof
console.log(c instanceof Array) =======> true
console.log(d instanceof Date) =======> true
console.log(f instanceof Function) =======> true
console.log(f instanceof function) =======> false
注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
根据对象的constructor判断: constructor
console.log(c.constructor === Array) ----------> true
console.log(d.constructor === Date) -----------> true
console.log(e.constructor === Function) -------> true
注意: constructor 在类继承时会出错
eg:
function A(){};
function B(){};
A.prototype = new B(); //A继承自B
var aObj = new A();
console.log(aObj.constructor === B) -----------> true;
console.log(aObj.constructor === A) -----------> false;
而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
console.log(aObj instanceof B) ----------------> true;
言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:
aObj.constructor = A; //将自己的类赋值给对象的constructor属性
console.log(aObj.constructor === A) -----------> true;
console.log(aObj.constructor === B) -----------> false; //基类不会报true了;
繁琐的方法: prototype
在JavaScript中,想要判断某个对象值属于哪种内置类型,最靠谱的做法就是通过Object.prototype.toString方法.
console.log(Object.prototype.toString.call(a) === ‘[object String]’) -------> true;
console.log(Object.prototype.toString.call(b) === ‘[object Number]’) -------> true;
console.log(Object.prototype.toString.call(c) === ‘[object Array]’) -------> true;
console.log(Object.prototype.toString.call(d) === ‘[object Date]’) -------> true;
console.log(Object.prototype.toString.call(e) === ‘[object Function]’) -------> true;
console.log(Object.prototype.toString.call(f) === ‘[object Function]’) -------> true;
jquery.type()
测试中所用到的 jquery: verson 3.0.0
console.log('number:', jQuery.type(2));
console.log('string:', jQuery.type('test'));
console.log('true or false :', jQuery.type(true));
console.log('undefined :', jQuery.type(undefined));
console.log('', jQuery.type());
console.log('null:', jQuery.type(null));
console.log('new Date():', jQuery.type(new Date()));
console.log('Function:', jQuery.type(function() {}));
console.log('Array:', jQuery.type([]));
console.log('Object:', jQuery.type({}));
console.log('new Error():', jQuery.type(new Error()));
console.log('reg:', jQuery.type(/test/));
console.log('ES6 新语法:symbol:', jQuery.type(Symbol()));
[参考链接]:https://www.cnblogs.com/dushao/p/5999563.html
JS数据类型判断的方法的更多相关文章
- JS数据类型判断的几种方法
JS数据类型判断 JavaScript 中常见数据类型有Number.String.Boolean.Object.Array.Json.Function.Date.RegExp.Error.undef ...
- js数据类型判断和数组判断
这么基础的东西实在不应该再记录了,不过嘛,温故知新~就先从数据类型开始吧 js六大数据类型:number.string.object.Boolean.null.undefined string: 由单 ...
- js数据类型 判断
1. js数据类型(两种数据类型) 基本数据类型:null undefined number boolean symbol string 引用数据类型: array object null: 空对象 ...
- 鉴别JS数据类型的全套方法
ECMAScript 标准定义了 7 种数据类型:Boolean.Null.Undefined.Number.String.Symbol(ES6新增)和Object,除Object以外的那6种数据类型 ...
- JS数组判断,方法
怎么判断一个对象是不是数组? 首先可以用 ES5 提供的 isArray 方法进行判断(注意:Array.isArray是ES 5.1推出的,不支持IE6~8,所以在使用的时候也应注意兼容问题. ) ...
- js 数据类型判断
判断type类型 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } ...
- js类型判断的方法
var arr=[]; alert(Object.prototype.toString.call(arr)=='[object Array]');
- js数据类型判断
在一般情况下使用typeof 但是有时候typeof返回的结果都是object,比如数组和json对象的时候,这个时候需要用到 instanceof了 还有一个更好得办法,Object.prototy ...
- vue—你必须知道的 js数据类型 前端学习 CSS 居中 事件委托和this 让js调试更简单—console AMD && CMD 模式识别课程笔记(一) web攻击 web安全之XSS JSONP && CORS css 定位 react小结
vue—你必须知道的 目录 更多总结 猛戳这里 属性与方法 语法 计算属性 特殊属性 vue 样式绑定 vue事件处理器 表单控件绑定 父子组件通信 过渡效果 vue经验总结 javascript ...
随机推荐
- 卸载ie
今天卸载ie11失败,最后使用下面这个命令实现了卸载,记录下 IE11卸载命令如下: FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Wind ...
- RTX参数配置
RTX操作系统的配置工作是通过配置文件RTX_Conf_CM.c实现. 在MDK工程中打开文件RTX_Conf_CM.c,可以看到如下图5.2所示的工程配置向导: 20 Task C ...
- Ionic异常及解决
1. 编译时提示: ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontV ...
- [原]Chef_Server and Chef_WorkStation and Chef_Client Install Guide[by haibo]
一.Prerequisite OS : CentOS-7.0-1406-x86_64-DVD.iso Time Server : NTP Server SERVER NAME IP PLAN ...
- array_rand
array_rand — 从数组中随机取出一个或多个单元 mixed array_rand ( array $array [, int $num = 1 ] ) 从数组中取出一个或多个随机的单元,并返 ...
- PE、ELF结构图
PE:https://bbs.pediy.com/thread-203563.htm ELF:https://blog.csdn.net/jiangwei0910410003/article/deta ...
- mac 远程连接 云服务器
之前mac 命令行连接云端服务器,一直失败,今天问题突然间解决了,如果遇到类似的问题,按照方法解决不了,可以在下面留言,共同探讨. 首先,在云端先判断一下云端服务器是否安装了 ssh服务器:op ...
- 严重: A child container failed during start的问题解决方法
找到tomcat中的server.xml中的文件, 将图中阴影的部分注释掉,即可.
- jar命令打jar包
jar -cvfM0 cloudwarehouse-enter.jar ./BOOT-INF ./META-INF ./org jar -cvfM0 xxl-job-admin.war ./BOOT- ...
- liunx系统问题总结
1.Unable to locate package错误 解决办法 :输入命令 sudo apt-get update,进行软件的更新