一,

自己有时候写一些东西,要做类型判断,还有测试的时候,对于原生的和jQuery中的类型判断,实在不敢恭维,所以就写了一个好用的类型判断,一般情况都够用的。

 function test(type) {
if(type === null || type === undefined) {
return type;
}
// 如果是对象,就进里面判断,否则就是基本数据类型
else if (type instanceof Object) { // js对象判断, 由于typeof不能判断null object,所以只能提前判断,互相补充
if(type instanceof Function) {
return 'Function';
}else if(type instanceof Array) {
return 'Array';
} else if(type instanceof RegExp){
return 'RegExp';
}else if(type instanceof Date) {
return 'Date';
}
// 判断是节点对象还是JQuery对象,很有用,新手一般来说分不清什么是什么类型
else if(type instanceof Node){
return 'Node';
}else if(type instanceof jQuery){
return 'jQuery';
}
else{
return 'Object';
}
}
// 基本数据类型
else {
return typeof type;
}
}

二,

原生的代码限制很多,

typeof只能判断基本数据类型外加undefied,Function。null会判断为object,Object和Array会判断为Object。

instanceof 只能判断对象

=== 可以判断null,undefined。

把上面三种方式组合起来,才能判断这些基本的类型。我有加入了另外几种类型判断,对于新手和老手,都是不错的工具。希望大家喜欢吧!

js类型判断-丰富加好用的更多相关文章

  1. JS类型判断typeof PK {}.toString.call(obj)

    参考链接:https://www.talkingcoder.com/article/6333557442705696719 先看typeof <!doctype html> <htm ...

  2. 类型和原生函数及类型转换(二:终结js类型判断)

    typeof instanceof isArray() Object.prototype.toString.call() DOM对象与DOM集合对象的类型判断 一.typeof typeof是一个一元 ...

  3. 看jquery3.3.1学js类型判断的技巧

    需要预习:call , typeof, js数据类型 1. isFunction中typeof的不靠谱 源码: var isFunction = function isFunction( obj ) ...

  4. JS类型判断&原型链

    JS类型检测主要有四种 1.typeof Obj 2.L instanceof R 3.Object.prototype.toString.call/apply(); 4.Obj.constructo ...

  5. js类型判断:typeof与instanceof

    typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,function(函数),object(NULL,数组,对象),und ...

  6. js类型判断

    console.log('---------------------'); var a="string"; console.log(a); //string var a=1; co ...

  7. js类型判断及鸭式辨型

    目录 instanceof constructor 构造函数名字 鸭式辨型 三种检测对象的类方式: instanceof.constructor .构造函数名字 用法如下: 1)instanceof ...

  8. js类型判断的方法

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

  9. js 类型判断

随机推荐

  1. saltstack系列~第二篇

    一 简介:今天咱们来继续学习saltstack 二 命名和分组 1 命名规则 1 ID构成 机房-DB类型-角色(主/从)-IP地址 2 分组构成  分为master slave两组即可 2 分组规则 ...

  2. Django学习手册 - ORM choice字段 如何在页面上显示值

    在module操作过程中使用choice字段: 核心: obj.get_字段名_display 定义module 数据结构: class msg(models.Model): choice = ( ( ...

  3. python - 发送邮件(email模块(内置))

    发送邮件 import smtplib from email.mime.text import MIMEText #邮箱件内容 HTML = """ 发送邮件测试2,加密 ...

  4. Qt5.7 无法输入中文问题

    把libfcitxplatforminputcontextplugin.so复制到安装的Qt目录下的两个文件夹中 sudo apt install fcitx-frontend-qt5 sudo cp ...

  5. python中datetime与string的相互转换

    >>> import datetime >>> value = '2016-10-30 01:48:31' >>> datetime.strpti ...

  6. Dubbo重试次数

    服务超时后重试次数[retries],不包含第一次调用,0代表不重试 *我们应该在幂等方法上设置重试次数[查询.删除.修改],在非幂等方法上禁止设置重试次数. ★幂等:指多次运行方法所产生的最终效果是 ...

  7. Linux下的换行符\n\r以及txt和word文档的使用

    Linux doc WINDOWS下记事本编写的文档和LINUX下VIM或者GEDIT等编写的文档的不同! 例如WINDOWS下编写的SH脚本,放到LINUX下执行可能会出错. 解决方法: 原因是:W ...

  8. W-GAN系 (Wasserstein GAN、 Improved WGAN)

    学习总结于国立台湾大学 :李宏毅老师 WGAN前作:Towards Principled Methods for Training Generative Adversarial Networks  W ...

  9. gdb revert, Go to previous line in gdb

    Yes! With the new version 7.0 gdb, you can do exactly that! The command would be "reverse-step& ...

  10. nagios系列(六)之nagios实现对服务器cpu温度的监控

    1.安装硬件传感器监控软件sensors yum install -y lm_sensors* 2.运行sensors-detect进行传感器检测 ##一路回车即可 Do you want to ov ...