js 判断数据类型
来源:互联网 发布:sql字段的date相等 编辑:程序博客网 时间:2024/06/06 07:19
(原文链接:http://www.cnblogs.com/a546558309/p/3608194.html)
typeof 用来判断各种数据类型,有两种写法:typeof xxx ,typeof(xxx)
如下实例:
typeof 2 输出 number
typeof null 输出 object
typeof {} 输出 object
typeof [] 输出 object
typeof (function(){}) 输出 function
typeof undefined 输出 undefined
typeof '222' 输出 string
typeof true 输出 boolean
这里面包含了js里面的五种数据类型 number string boolean undefined object和函数类型 function
看到这里你肯定会问了:我怎么去区分对象,数组和null呢?
接下来我们就用到另外一个利器:Object.prototype.toString.call
这是对象的一个原生原型扩展函数,用来更精确的区分数据类型。
我们来试试这个玩儿意儿:
var gettype=Object.prototype.toString
gettype.call('aaaa') 输出 [object String]
gettype.call(2222) 输出 [object Number]
gettype.call(true) 输出 [object Boolean]
gettype.call(undefined) 输出 [object Undefined]
gettype.call(null) 输出 [object Null]
gettype.call({}) 输出 [object Object]
gettype.call([]) 输出 [object Array]
gettype.call(function(){}) 输出 [object Function]
看到这里,刚才的问题我们解决了。
- js中数据类型判断
- js数据类型的判断
- js数据类型判断
- js判断数据类型
- js中数据类型判断
- 判断js数据类型
- js判断数据类型
- JS判断数据类型
- js如何判断数据类型
- JS判断数据类型
- JS判断数据类型
- js判断数据类型
- 判断js数据类型
- js 判断各种数据类型
- 判断js中的数据类型
- js 判断各种数据类型
- js判断数据类型
- $.type(),js数据类型判断
- 类似淘宝商品展示,上面图片下面名字和标题的css样式设置
- 实现数据库的批处理两种方式,以及比较其优缺点
- Android开发中 MVP模式
- iOS关于导航控制器
- 面试算法学习-4-字符串全排列
- js 判断数据类型
- 带EditText的AlertDialog 拿来就用
- LeetCode-Array-121. Best Time to Buy and Sell Stock
- 使用 Vue.js 2.0 框架开发和运行
- windows下和linux下的git环境的配置
- DVWA - SQL Injection (Blind) (low)
- Shell中判断字符串是否为数字的6种方法
- LeetCode第五关:Longest Palindromic Substring --> Difficulty: Medium
- C++:编译最常见的错误