js中类型判断
来源:互联网 发布:淘宝买家也可以开店吗 编辑:程序博客网 时间:2024/06/03 21:48
JS判断数据类型
js基本类型有 Undefined、Null、Boolean、Number和String,Object(由名值对集合)
typeof判断类型
var nullValue = null,boolValue = true,nmValue=12,strValue = 'test'; var objValue={x:y},arrayValue = [1,23,3], functionValue = function(){ console.info('function') }; typeof nullValue == 'object';//true typeof boolValue == 'boolean';//true typeof nmValue == 'number';//true typeof strValue == 'string';//true typeof objValue== 'object';//true typeof unValue== 'undefined';//true typeof arrayValue == 'object';//true typeof functionValue == 'function';//true typeof NaN//"number" /* *typeof 只能准确判断Undefined、Boolean、Number和String类型和function *对Null,Object,Array测试其值为'object' **/
instanceof判断
instanceof 判断已知对象,主要用于判断Array,object等
[1,2] instanceof Array;//true 数组 new Date() instanceof Date;//Data var Obj = function(){} Obj.prototype.info = function(){ console.info(Array.prototype.slice.apply(argument,[1])) } var testObj = new Obj(); testObj instanceof Obj;//true 自定义对象 var fucObj = function(){}; fucObj instanceof Function;//true 函数判断 /* *instanceof 适合对象判断 **/
constructor判断
适用于对象的判断
false.constructor === Boolean;//true 'test'.constructor === String;//true [1,23].constructor === Array;//true var obj = {x:1} obj .constructor === Object;//true var funtest = function(){} funtest .constructor === Function;//true new Date().constructor === Date;//true //对于继承 var A = function(){}; var B = function(){}; A.prototype = new B(); var a = new A(); a.constructor === B;//true a.constructor === A;//false /* *此处对于instanceof 能胜任判断 **/
Object.prototype.toString.apply()判断
"[Object Array]" === Object.prototype.toString.apply([]);//true "[object Object]"=== Object.prototype.toString.apply({});//true "[object String]" === Object.prototype.toString.apply('123');//true "[object Number]" === Object.prototype.toString.apply(123);//true "[object Boolean]" === Object.prototype.toString.apply(false);//true "[object Date]" === Object.prototype.toString.apply(new Date());// "[object Null]" === Object.prototype.toString.apply(null);//true "[object Undefined]" === Object.prototype.toString.apply(undefined);//true
1 0
- js中类型判断
- js中类型判断
- JS中判断浏览器类型
- js中判断浏览器类型
- js中变量类型判断
- 在JS中判断浏览器的类型
- 判断js中对象的类型
- 在JS中判断浏览器的类型
- js中判断对象具体类型
- 在JS中判断浏览器的类型
- JS中判断undefined和null类型
- js/javascript中判断对象类型
- js中typeof只能判断基础类型。
- 判断js中对象的类型
- js中undefined类型的判断
- 如何判断js中对象的类型
- js中判断对象具体类型
- JS中Boolean类型值的判断
- 二叉树的层次遍历
- 三,资源初始化操作和主菜单界面的创建
- 一键拨号,发短信,QQ,百度地图导航
- 进程重启后,泄露的内存能否回收
- 图的邻接表的遍历(DFS(递归,非递归),BFS,拓扑排序)
- js中类型判断
- MFC对话框内控件大小随着对话框大小变化
- JAVA的反射机制
- FTP环境搭建及客户代码调用公共方法封装
- 在struts2中使用filter来配置servlet处理业务
- java基础-面向对象
- 使用python访问网站遇到的问题
- Android Windows悬浮窗
- EditPlus如何开启Emmet插件