JS中的数据

本文介绍了JavaScript中的数据类型,包括基本数据类型和复杂数据类型,以及如何获取数据类型。详细讲解了Array、Number、String、Date、Map和Json等数据类型的方法,如Array的concat、slice、sort等,Number的toString、toFixed,以及String的length、indexOf、replace等。还讨论了数据遍历的各种方式,如普通for循环、forEach、for...in、map和for...of。
摘要由CSDN通过智能技术生成
  • 一、数据类型
    • 项目
      • 项目
  • 二、获取数据类型
  • 三、数据所对应的方法
    • 一、Array
    • 二、Number
    • 三、String
    • 四、Date
    • 五、Map
    • 六、Json
  • 四、数据遍历

一、数据类型

基本数据类型:Undefined,Null,Boolean,Number,String
复杂数据类型:Object
数据的集合 : Array

二、获取数据类型

  1. typeof
    返回类型为字符串格式,可以判断function的类型,在判断除Object类型的对象时比较方便。
alert(typeof 123)         “number”
alert(typeof [“1”,”2”])   “object”
alert(typeof null)        “object” 
alert(typeof undefined)   “undefined”
  1. instanceof
    后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
alert(123 instanceof Number)  返回true
  1. 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:

  1. 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
  1. 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值