最常用的判断方法: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数据类型判断的方法的更多相关文章

  1. JS数据类型判断的几种方法

    JS数据类型判断 JavaScript 中常见数据类型有Number.String.Boolean.Object.Array.Json.Function.Date.RegExp.Error.undef ...

  2. js数据类型判断和数组判断

    这么基础的东西实在不应该再记录了,不过嘛,温故知新~就先从数据类型开始吧 js六大数据类型:number.string.object.Boolean.null.undefined string: 由单 ...

  3. js数据类型 判断

    1. js数据类型(两种数据类型) 基本数据类型:null undefined number boolean symbol string 引用数据类型: array object null: 空对象 ...

  4. 鉴别JS数据类型的全套方法

    ECMAScript 标准定义了 7 种数据类型:Boolean.Null.Undefined.Number.String.Symbol(ES6新增)和Object,除Object以外的那6种数据类型 ...

  5. JS数组判断,方法

    怎么判断一个对象是不是数组? 首先可以用 ES5 提供的 isArray 方法进行判断(注意:Array.isArray是ES 5.1推出的,不支持IE6~8,所以在使用的时候也应注意兼容问题. ) ...

  6. js 数据类型判断

    判断type类型 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } ...

  7. js类型判断的方法

    var arr=[]; alert(Object.prototype.toString.call(arr)=='[object Array]');

  8. js数据类型判断

    在一般情况下使用typeof 但是有时候typeof返回的结果都是object,比如数组和json对象的时候,这个时候需要用到 instanceof了 还有一个更好得办法,Object.prototy ...

  9. vue—你必须知道的 js数据类型 前端学习 CSS 居中 事件委托和this 让js调试更简单—console AMD && CMD 模式识别课程笔记(一) web攻击 web安全之XSS JSONP && CORS css 定位 react小结

    vue—你必须知道的   目录 更多总结 猛戳这里 属性与方法 语法 计算属性 特殊属性 vue 样式绑定 vue事件处理器 表单控件绑定 父子组件通信 过渡效果 vue经验总结 javascript ...

随机推荐

  1. tensorflow 调试tfdbg

    1.执行pip install pyreadline 安装pyreadline 2.修改对应代码如下 with tf.Session() as sess: sess.run(tf.global_var ...

  2. LinkedBlockingQueue源码分析

    1. LinkedBlockingQueue源码分析(JDK8) 2. LinkedBlockingQueue源码分析 啦啦啦

  3. Ubuntu 14.04 LTS 系统空间不足,输入密码后,无法进入桌面的解决办法

    问题场景:系统空间不足,在没有清理空间的情况下,重启机器,可能会出现,输入密码后,无法进入桌面的现象. 如何解决? 解决方法:以访客身份登录系统,可以看到你的ip地址,然后以ssh的方式登录你的主机, ...

  4. Install Local SQL In Mac OS

    extends:http://www.cnblogs.com/maxinliang/p/3583702.html 一.安装 到MySQL官网上http://dev.mysql.com/download ...

  5. VS2015编译rtklib2.4.2

    准备工作 在VS2015下新建一个win32的dll项目(空项目) 把在github上下载的rtklib2.4.2里的src文件夹复制到刚刚建立的win32下 把src里的文件添加到项目里,其中头文件 ...

  6. Python学习之旅(二十)

    Python基础知识(19):面向对象高级编程(Ⅱ) 定制类 形如“__xx__”的变量或函数在Python中是有特殊用途的 1.__str__ 让打印出来的结果更好看 __str__:面向用户:__ ...

  7. javascript的数组之sort()

    sort()方法用in-place的算法对原数组进行排序,但不会产生新的数组.这个方法不是一个稳定的排序,默认采用的是安字符串Unicode码点进行排序的. let fruit =  ['cherri ...

  8. 洛谷P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  9. ext 的controller中的refs的使用方法

    通过ext api 可以知道ext 的controller中有个refs的属性,对于这个属性 文档上是这么说的:配置数组构建页面上的视图的引用. 我并看不懂,接下来说的是我对这个refs的理解. 对这 ...

  10. vue 点击弹窗外框关闭弹框

    https://blog.csdn.net/zjw0742/article/details/77822777 ready() { document.addEventListener('click', ...