常见的判断数据类型的几种方法 -- jsvascript
发布日期:2021-05-11 02:04:59 浏览次数:12 分类:精选文章

本文共 4112 字,大约阅读时间需要 13 分钟。

ECMAScript ���������������������������������

ECMAScript ������

ECMAScript ��������������� 7 ��������������������������������������������������������������������������������� String���Null���Undefined���Number���Boolean���Symbol��������������������������� Object���Function���Array���RegExp���Date ������

������������������

  • ���������������������String���Null���Undefined���Number���Boolean���Symbol
  • String������������������������������������������������������
  • Null������������������������ JavaScript ������ null ���������������������
  • Undefined���������������������������������
  • Number���������������������
  • Boolean������������������
  • Symbol������ ES6 ������������������������������������������������������������������������

������������������

  • Object���������������
  • Function���������
  • Array���������
  • RegExp������������������
  • Date���������������

��������� Symbol

Symbol ������������������������������������������������������������������ ES6 ������������������������������������������������������������������������

const symbol = Symbol('myKey'); // ���������������������������

������������������������

1. typeof ���������

  • ������������������'number'���'boolean'���'symbol'���'string'���'object'���'undefined'���'function'
  • ������������������typeof a ��������� undefined
  • ���������null ��� typeof ������ 'object'������������������������

typeof ������������

  • ������QDebug��������������������������������������������� 'object'
  • ������������������������instanceof���toString.call���������������

2. instanceof ���������

������������������������������������������������������������������������������ instanceof ���������������������������

  • ��� HTML ������������������arr instanceof Array
  • ���������������������������-letter���������-L���������������
  • ���������������������������
���������console.log([] instanceof Array); // true

3. Object.prototype.toString.call() ������

������������������������������ [Class] ������������������ [object Xxx]������������������

  • string��������� [object String]
  • number���[object Number]
  • boolean���[object Boolean]
  • symbol���[object Symbol]
  • date���[object Date]
  • array���[object Array]
  • regexp���[object RegExp]
���������������console.log(Object.prototype.toString.call(new Date())); // [object Date]

4. constructor ������

������������������������������������������������������

  • constructor ��������������������������� Object
  • null���undefined ��� constructor
  • ��������������������������������������� prototype ��� constructor ������
���������console.log(arr.constructor === Array); // true

5. jQuery ������������������������������

  • jQuery.isArray()���������������
  • jQuery.isEmptyObject()���������������������������
  • jQuery.isFunction()������������������������
  • jQuery.isPlainObject()������������������������������
  • jQuery.isWindow()������������������ window ������
  • jQuery.isXMLDoc()������������������ XML ������������
上一篇:hooks初入门了解
下一篇:前端打包究竟是什么

发表评论

最新留言

不错!
[***.144.177.141]2025年04月08日 09时27分56秒