- 一、数据类型
- 项目
- 项目
- 项目
- 二、获取数据类型
- 三、数据所对应的方法
- 一、Array
- 二、Number
- 三、String
- 四、Date
- 五、Map
- 六、Json
- 四、数据遍历
一、数据类型
基本数据类型:Undefined,Null,Boolean,Number,String
复杂数据类型:Object
数据的集合 : Array
二、获取数据类型
- typeof
返回类型为字符串格式,可以判断function的类型,在判断除Object类型的对象时比较方便。
alert(typeof 123) “number”
alert(typeof [“1”,”2”]) “object”
alert(typeof null) “object”
alert(typeof undefined) “undefined”
- instanceof
后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
alert(123 instanceof Number) 返回true
- constructor
在类继承时会出错
function A(){};
function B(){};
A.prototype = new B(); //A继承自B
var aobj = new A();
alert(aobj.constructor === B) -----------> true;
alert(aobj.constructor === A) -----------> false;
而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
- prototype
alert(Object.prototype.toString.call("string")==='[object String]'); -------> true
alert(Object.prototype.toString.call(123)==='[object Number]'); -------> true
alert(Object.prototype.toString.call([1,2,3])==='[object Array]'); -------> true
- jQuery.type()
如果对象是undefined或null,则返回相应的“undefined”或“null”。
jQuery.type( undefined ) === "undefined"
jQuery.type() === "undefined"
jQuery.type( window.notDefined ) === "undefined"
jQuery.type( null ) === "null"
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 E