js类型判断-丰富加好用
一,
自己有时候写一些东西,要做类型判断,还有测试的时候,对于原生的和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类型判断-丰富加好用的更多相关文章
- JS类型判断typeof PK {}.toString.call(obj)
参考链接:https://www.talkingcoder.com/article/6333557442705696719 先看typeof <!doctype html> <htm ...
- 类型和原生函数及类型转换(二:终结js类型判断)
typeof instanceof isArray() Object.prototype.toString.call() DOM对象与DOM集合对象的类型判断 一.typeof typeof是一个一元 ...
- 看jquery3.3.1学js类型判断的技巧
需要预习:call , typeof, js数据类型 1. isFunction中typeof的不靠谱 源码: var isFunction = function isFunction( obj ) ...
- JS类型判断&原型链
JS类型检测主要有四种 1.typeof Obj 2.L instanceof R 3.Object.prototype.toString.call/apply(); 4.Obj.constructo ...
- js类型判断:typeof与instanceof
typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,function(函数),object(NULL,数组,对象),und ...
- js类型判断
console.log('---------------------'); var a="string"; console.log(a); //string var a=1; co ...
- js类型判断及鸭式辨型
目录 instanceof constructor 构造函数名字 鸭式辨型 三种检测对象的类方式: instanceof.constructor .构造函数名字 用法如下: 1)instanceof ...
- js类型判断的方法
var arr=[]; alert(Object.prototype.toString.call(arr)=='[object Array]');
- js 类型判断
随机推荐
- Android的网络通信机制
1. Socket接口 不常用 2.HttpURLConnection接口 3. HttpClient接口 http://blog.csdn.net/ccc20134/article/details/ ...
- Linux查找文件、文件夹
https://www.jianshu.com/p/f3a46e5c96ba 查找目录:find /(查找范围) -name '查找关键字' -type d 查找文件:find /(查找范围) -na ...
- Spring之JDBCTemplate学习
一.Spring对不同的持久化支持: Spring为各种支持的持久化技术,都提供了简单操作的模板和回调 ORM持久化技术 模板类 JDBC org.springframework.jdbc.core. ...
- 【转】Python介绍
[转]Python介绍 本节内容 Python简史 Python是一门什么样的语言? Python的优点与缺点 Python解释器 一.Python简史 历史背景 在20世纪80年代,IBM和苹果已经 ...
- python3 三元表达式,列表解析
python3 三元表达式,列表解析 三元表达式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 x=2 y=3 if x > y ...
- Kaggle 泰坦尼克
入门kaggle,开始机器学习应用之旅. 参看一些入门的博客,感觉pandas,sklearn需要熟练掌握,同时也学到了一些很有用的tricks,包括数据分析和机器学习的知识点.下面记录一些有趣的数据 ...
- 如何在linux下检测内存泄漏(转)
本文转自:http://www.ibm.com/developerworks/cn/linux/l-mleak/ 本文针对 linux 下的 C++ 程序的内存泄漏的检测方法及其实现进行探讨.其中包括 ...
- gulp自动化构建教程
gulp及gulpfile.js编写示例 本文主要记录一个gulpfile.js示例,以免以后用的时候遗忘.但首先还是要了解gulp是什么以及如何使用. 一.什么是gulp 简单来说:就是压缩前 ...
- jquery实现星级评分
项目中遇到到实现星级评分,就用了这个插件 http://www.jq22.com/jquery-info291
- eol-last的相关知识
eslint “eol-last”:0 文件末尾强制换行(就是代码结尾处,要来个空格,相当于加一行,设置为0就可以了) ./src/main.js error eol-last Newline ...