博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
11判断js中的数据类型的几种方法
阅读量:5286 次
发布时间:2019-06-14

本文共 2994 字,大约阅读时间需要 9 分钟。

来源:https://www.cnblogs.com/dushao/p/5999563.html 1、typeof,返回值是字符串并且首字母小写,可以判断function数据类型,在判断非Object类型的数据时比较方便。
var myString = "he is string";var myNumber = 222;var myArray= [1,2,3];var myDate = new Date();var myObject = {a:1};var myFunction = function(){console.log(111);};console.log(typeof myString)   // stringconsole.log(typeof myNumber)   // numberconsole.log(typeof myArray)   // objectconsole.log(typeof myDate)   // objectconsole.log(typeof myObject)   // objectconsole.log(typeof myFunction)   // functionconsole.log(typeof myNumber == "number") // true
2、instanceof,返回值是布尔并且首字母小写
var myString = "he is string";var myNumber = 222;var myArray= [1,2,3];var myDate = new Date();var myObject = {a:1};var myFunction = function(){console.log(111);};console.log(myString instanceof String) // trueconsole.log(myNumber instanceof Number) // trueconsole.log(myArray instanceof Array) // trueconsole.log(myDate instanceof Date) // trueconsole.log(myObject instanceof Object) // trueconsole.log(myFunction instanceof Function) // true 
3、constructor,返回值是对象并且首字母大写
var myString = "he is string";var myNumber = 222;var myArray= [1,2,3];var myDate = new Date();var myObject = {a:1};var myFunction = function(){console.log(111);};console.log(myString.constructor === String) // trueconsole.log(myNumber.constructor === Number) // trueconsole.log(myArray.constructor === Array) // trueconsole.log(myDate.constructor === Date) // trueconsole.log(myObject.constructor === Object) // trueconsole.log(myFunction.constructor === Function) // true 
4、prototype,返回值是(数组)字符串。
var myString = "he is string";var myNumber = 222;var myArray= [1,2,3];var myDate = new Date();var myObject = {a:1};var myFunction = function(){console.log(111);};console.log(Object.prototype.toString.call(myString) === "[object String]") // true;console.log(Object.prototype.toString.call(myNumber) === "[object Number]") // true;console.log(Object.prototype.toString.call(myArray) === "[object Array]") // true;console.log(Object.prototype.toString.call(myDate) === "[object Date]") // true;console.log(Object.prototype.toString.call(myObject) === "[object Object]") // true;console.log(Object.prototype.toString.call(myFunction) === "[object Function]") // true;
5、jquery.type()
var myUndefined=undefined;var myNull=null;var myString = "he is string";var myNumber = 222;var myArray= [1,2,3];var myDate = new Date();var myObject = {a:1};var myFunction = function(){console.log(111);};var myError = new Error();var myRegexp = /test/;console.log(jQuery.type(myUndefined) === "undefined");console.log(jQuery.type(myNull) === "null");console.log(jQuery.type(myString) === "string");console.log(jQuery.type(myNumber) === "number");console.log(jQuery.type(myArray ) === "array");console.log(jQuery.type(myDate) === "date");console.log(jQuery.type(myFunction) === "function");console.log(jQuery.type(myError) === "error");console.log(jQuery.type(myRegexp) === "regexp");

  

 

 

转载于:https://www.cnblogs.com/gushixianqiancheng/p/10964005.html

你可能感兴趣的文章
QTP函数使用(实战系列三)
查看>>
解决eclipse building workspace慢的情况
查看>>
Linux系统启动详解(一)
查看>>
web应用BaseDao及BaseDaoImpl
查看>>
word怎么在方框中打对号
查看>>
PHP扩展--vld查看opcode代码
查看>>
查看oracle数据库编码:
查看>>
提供他人class文件
查看>>
spring与quartz定时器
查看>>
[c#]asp.net开发微信公众平台(8)微信9大高级接口,自定义菜单
查看>>
云计算技术了解
查看>>
【Android】Kill Service
查看>>
处理高并发,防止库存超卖
查看>>
线性表(不间断更新)
查看>>
点与线的距离及垂足点
查看>>
『原创』关于“手机拍照识别二维码”WM版运行中的小问题及解决~
查看>>
@one to many 和 @many to one小例子
查看>>
someone you loved 歌词翻译
查看>>
使用startActivityForResult场景模拟
查看>>
待解决问题
查看>>