判断js中数据类型方法总结
来源:互联网 发布:网络监控如何连接电脑 编辑:程序博客网 时间:2024/06/05 01:51
判断js中数据类型方法总结
1. 常用的方法与定义数据
typeof
、instanceof
、constructor
、prototype
、$.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方法
- js中判断数据类型的方法总结
- 判断js中数据类型方法总结
- js中数据类型判断
- js中数据类型判断
- js中判断数据类型
- JS 判断数据类型的方法
- js中判断数据类型对的几种方法
- JS中判断数据类型的几种方法
- JS中数据类型的判断
- js中数据类型的判断
- js中数据类型的判断
- js判断数据类型三种方法
- JS判断数据类型检查的方法
- js 判断各种数据类型的简单方法
- JS判断数据类型的三种方法
- 判断JS数据类型的四种方法
- 判断JS数据类型的四种方法
- 判断JS数据类型的四种方法
- POJ 3061 · Subsequence【子序列】【尺取法】
- caller和callee的区别
- ContentProvider实现程序之间共享数据(如短信、联系人)
- 图像处理git仓库
- 不定宽高div水平垂直居中(兼容ie6)
- 判断js中数据类型方法总结
- Comparable和Comparator的区别
- 在三个或多个界面里边按返回按钮如何回到主界面?singleTask
- JavaScript性能提升学习
- ubuntu启动后桌面只有壁纸,其余啥也没有
- TensorFlow 中文文档
- onload与ready对比
- Sublime Text3 解决中文乱码+汉化【最简单教程】
- js中三种URI编码方式比较