当前位置: 首页 > news >正文

象山网站优化公司建设网站开发方案

象山网站优化公司,建设网站开发方案,WordPress潮流媒体主题,游戏网站代码数据类型与判断 文章目录 数据类型与判断**1. JS 的数据类型有哪些?****2. null 和 undefined 的区别****3. 如何判断数据类型****(1) typeof****(2) Array.isArray()****(3) instanceof****(4) Object.prototype.toString.call()****(5) 自定义工具函数** **4. 常见…

数据类型与判断


文章目录

      • 数据类型与判断
        • **1. JS 的数据类型有哪些?**
        • **2. `null` 和 `undefined` 的区别**
        • **3. 如何判断数据类型**
          • **(1) `typeof`**
          • **(2) `Array.isArray()`**
          • **(3) `instanceof`**
          • **(4) `Object.prototype.toString.call()`**
          • **(5) 自定义工具函数**
        • **4. 常见问题与陷阱**
        • **总结**
      • **总结表:类型判断方法对比**

1. JS 的数据类型有哪些?

JavaScript 数据类型分为 基本数据类型引用数据类型

类型说明示例
基本数据类型
number整数、浮点数、InfinityNaN42, 3.14, NaN
string字符串(单引号、双引号、模板字符串)'hello', "world"
boolean布尔值true, false
null表示空值(通常由程序员显式赋值)let a = null;
undefined变量未初始化时的默认值let b;undefined
symbol唯一且不可变的值(ES6 新增,用于对象属性键)Symbol('key')
bigint大整数(ES2020 新增,用于精确表示大数)123n
引用数据类型
object对象、数组、函数、日期等{}, [], function(){}

2. nullundefined 的区别
特性nullundefined
含义显式表示“空值”变量未初始化时的默认值
类型typeof null'object'typeof undefined'undefined'
赋值场景程序员主动赋空值变量声明未赋值时自动获得
相等性null == undefinedtruenull === undefinedfalse
典型用途清空对象引用变量未初始化或函数无返回值

示例

let a;
console.log(a); // undefined(未赋值)let b = null;
console.log(b); // null(主动赋值)

3. 如何判断数据类型
(1) typeof
  • 返回值的字符串表示(如 "number""string")。

局限性:

  • typeof null"object"(历史遗留问题)。
  • 无法区分 object 的具体类型(如数组、日期)。

示例

typeof 123;        // "number"
typeof "abc";      // "string"
typeof true;       // "boolean"
typeof undefined;  // "undefined"
typeof {};         // "object"
typeof [];         // "object"
typeof null;        // 'object'
typeof function(){}; // "function"
typeof Symbol();   // "symbol"
typeof 10n;        // "bigint"

(2) Array.isArray()
  • 专门用于判断是否为数组。
  • 优势:比 instanceof 更可靠(跨 iframe/窗口时有效)。

示例

Array.isArray([1, 2]);     // true
Array.isArray({ a: 1 });   // false

(3) instanceof
  • 判断对象是否是某个构造函数的实例。
  • 局限性:跨上下文(如 iframe)时可能失效。

示例

[] instanceof Array;        // true
{} instanceof Object;       // true
(function() {}) instanceof Function; // true
'str' instanceof String; // false(基本数据类型无法用 instanceof)

(4) Object.prototype.toString.call()
  • 通用方法,返回 [object Type]
  • 优势:可准确判断所有类型。

示例

Object.prototype.toString.call(123);        // "[object Number]"
Object.prototype.toString.call("abc");      // "[object String]"
Object.prototype.toString.call(true);       // "[object Boolean]"
Object.prototype.toString.call(undefined);  // "[object Undefined]"
Object.prototype.toString.call(null);       // "[object Null]"
Object.prototype.toString.call([]);         // "[object Array]"
Object.prototype.toString.call({});         // "[object Object]"

(5) 自定义工具函数

结合上述方法封装通用类型判断:

function getType(value) {return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}getType(123);        // "number"
getType("abc");      // "string"
getType([]);         // "array"
getType({});         // "object"
getType(null);       // "null"
getType(undefined);  // "undefined"

4. 常见问题与陷阱
  1. typeof null 返回 "object"
    • 这是 JavaScript 的历史设计错误,需通过 value === null 判断。
  2. 区分数组和普通对象
    • 推荐使用 Array.isArray()Object.prototype.toString.call()
  3. 跨上下文类型判断
    • instanceof 在跨 iframe 时可能失效,需用其他方法。

总结
判断目标推荐方法
基本类型typeof
数组Array.isArray()Object.prototype.toString.call()
nullvalue === null
undefinedvalue === undefined
通用类型判断Object.prototype.toString.call()

总结表:类型判断方法对比

方法适用场景示例
typeof基本数据类型类型(null 除外)typeof 'str''string'
Array.isArray()判断数组Array.isArray([])true
instanceof对象实例判断(不跨全局环境)[] instanceof Arraytrue
判断数组Array.isArray([])true
instanceof对象实例判断(不跨全局环境)[] instanceof Arraytrue
Object.prototype.toString通用类型判断toString.call(null)'[object Null]'
http://www.dtcms.com/a/448769.html

相关文章:

  • 做网站建设公司crm在线广东的一起做网站
  • 相册特效手机网站阿里云虚拟主机多网站
  • 电商网站活动推广门户网站建设工作管理办法
  • 免费网站推广渠道怎样给自己的店做网站
  • 音乐盒网站源码树莓派wordpress
  • 网站更换目录名如何做301跳转新人如何自学做网站
  • 邹城网站定制公司域名备案怎么弄
  • 建设学校网站中山市路桥建设有限公司网站
  • 做一个网站大概要多少钱做搬家服务网站问卷调查结果
  • 宿州网站建设开发公司哪家好在百度云上做网站
  • 贵州住房和城乡建设厅网站朗朗上口的广告公司名称
  • 国际婚恋网站做翻译合法吗网站开发的技术风险
  • 怎样做网站系统福州网站建设费用
  • 哔哩哔哩网站免费进入seo关键词优化推广价格
  • 做坏事网站大气个人网站源码
  • 中国建设局网站首页爱站网ip反域名查询
  • 怎么用记事本做网站企业网站整合
  • 图片网站源码网站营销的优缺点
  • 网站建设专业输入法17网一起做网店广州货源网
  • 动态购物网站开发源代码wordpress 点击文章
  • 网站内页标题修改前端做网站直播
  • 网站后续建设外贸 国外推广网站
  • 外管局网站先支后收怎么做报告怎样做网站导购
  • 医院管理系统网站模板广州免费景点
  • 网站开发设计中的收获企业网站建设对网络营销的影响主要表现在( )
  • 用自己的电脑做服务器搭建网站局域网网站建设的步骤过程
  • 大通县公司网站建设大安区网站建设
  • phpmysql网站开发实例手机建设网站公司
  • android网站开发视频软文推广做得比较好的推广平台
  • 网站开发与设计这么样谷歌seo运营