前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >判断js中数据类型方法总结

判断js中数据类型方法总结

作者头像
csxiaoyao
发布2019-02-15 16:41:05
8.3K2
发布2019-02-15 16:41:05
举报
文章被收录于专栏:csxiaoyaocsxiaoyao

判断js中数据类型方法总结

1. 常用的方法与定义数据

typeofinstanceofconstructorprototype$.type()/jquery.type() 定义数据 ~ var a = "csxiaoyao"; var b = 1993; var c = true; var d = undefined; var e = null; var f = new Date(); var g= [1,2,3]; var h = function(){alert("sunshine");}; var i = function(){this.name="csxiaoyao";}; ~

2. typeof 

typeof (返回的类型为字符串形式) ~ console.log(typeof a); // string console.log(typeof b); // number console.log(typeof c); // boolean console.log(typeof d); // undefined console.log(typeof e); // object console.log(typeof f); // object console.log(typeof g); // object console.log(typeof h); // function console.log(typeof i); // function console.log(typeof a == "string"); // true console.log(typeof a == String); // false ~

3. instanceof

instanceof (判断已知对象类型的方法) ~ // console.log(e instanceof null); // Right-hand side of 'instanceof' is not an object console.log(f instanceof Date); // true console.log(g instanceof Array); // true console.log(h instanceof Function); // true ~

4. constructor

constructor (根据对象的constructor判断) ~ console.log(f.constructor === Date); // true console.log(g.constructor === Array); // true console.log(h.constructor === Function); // true // 注意:constructor 在类继承时会出错 function A(){}; function B(){}; A.prototype = new B(); //A继承自B var aObj = new A(); console.log(aObj.constructor === A);// false console.log(aObj.constructor === B);// true // instanceof没有问题 console.log(aObj instanceof A);// true console.log(aObj instanceof B);// true // 解决construtor的问题通常让对象的constructor手动指向自己 aObj.constructor = A; //将自己的类赋值给对象的constructor属性 console.log(aObj.constructor === A);// true console.log(aObj.constructor === B);// false ~

5. prototype

prototype(通用但很繁琐的方法) ~ console.log(Object.prototype.toString.call(a) === '[object String]')// true console.log(Object.prototype.toString.call(b) === '[object Number]')// true console.log(Object.prototype.toString.call(c) === '[object Boolean]')// true console.log(Object.prototype.toString.call(d) === '[object Undefined]')// true console.log(Object.prototype.toString.call(e) === '[object Null]')// true console.log(Object.prototype.toString.call(f) === '[object Date]')// true console.log(Object.prototype.toString.call(g) === '[object Array]')// true console.log(Object.prototype.toString.call(h) === '[object Function]')// true ~ 大小写不能写错,比较麻烦,但胜在通用

6. jquery.type()

jquery.type() (万能方法) ~ // 如果对象是undefined或null,则返回相应的“undefined”或“null” jQuery.type( undefined ) === "undefined" jQuery.type() === "undefined" jQuery.type( window.notDefined ) === "undefined" jQuery.type( null ) === "null" // 如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,返回相应的 [[Class]] 名字 jQuery.type( true ) === "boolean" jQuery.type( 3 ) === "number" jQuery.type( "test" ) === "string" jQuery.type( function(){} ) === "function" jQuery.type( [] ) === "array" jQuery.type( new Date() ) === "date" jQuery.type( new Error() ) === "error" // as of jQuery 1.9 jQuery.type( /test/ ) === "regexp" // 其他一切都将返回类型“object” ~

7. 总结

通常情况下用typeof判断,遇到预知Object类型的情况可以选用instanceof或constructor方法

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年05月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 判断js中数据类型方法总结
    • 1. 常用的方法与定义数据
      • 2. typeof 
        • 3. instanceof
          • 4. constructor
            • 5. prototype
              • 6. jquery.type()
                • 7. 总结
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档