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

组建网站 多少钱优化建站

组建网站 多少钱,优化建站,水果网站模板,做淘宝客网站备案要怎么写在 JavaScript 中,数据类型检测是开发中的常见需求。以下是主要检测方法及其优缺点: 1. typeof 操作符 最基础的检测方式,返回类型字符串: typeof 42; // "number" typeof "hello"; // &qu…

在 JavaScript 中,数据类型检测是开发中的常见需求。以下是主要检测方法及其优缺点:

1. typeof 操作符

最基础的检测方式,返回类型字符串:

typeof 42;           // "number"
typeof "hello";      // "string"
typeof true;         // "boolean"
typeof undefined;    // "undefined"
typeof function() {}; // "function"
typeof Symbol();     // "symbol"
typeof {};           // "object"
typeof [];           // "object" (注意!)
typeof null;         // "object" (历史遗留问题)

缺点:

  • 无法区分数组、对象、null(都返回 “object”)

2. instanceof 操作符

检测对象是否属于某个构造函数的实例:

[] instanceof Array;      // true
{} instanceof Object;     // true
new Date() instanceof Date; // truefunction Person() {}
const p = new Person();
p instanceof Person;      // true

缺点:

  • 不适用于原始值类型(数字、字符串等)
  • 多窗口/iframe 环境下失效(不同全局环境)

3. Object.prototype.toString.call()

最可靠的检测方法,返回 [object Type] 格式:

Object.prototype.toString.call(42);        // "[object Number]"
Object.prototype.toString.call("hello");   // "[object String]"
Object.prototype.toString.call([]);        // "[object Array]"
Object.prototype.toString.call(null);      // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(/regex/);   // "[object RegExp]"

封装工具函数:

function getType(obj) {return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}getType([]); // "array"
getType(null); // "null"

4. 特殊类型检测

  • 数组检测:
Array.isArray([]); // true (ES5+ 最佳方案)
// 旧浏览器兼容方案:
Object.prototype.toString.call([]) === "[object Array]";
  • NaN 检测:
isNaN(NaN); // true (注意: isNaN("abc") 也返回 true)
Number.isNaN(NaN); // true (ES6 推荐,严格检测)
  • null/undefined 检测:
let foo = null;
foo === null; // true (精确检测 null)
typeof bar === "undefined"; // 检测未定义

终极解决方案

结合多种方式实现全类型检测:

function detectType(value) {// 处理 null 和 undefinedif (value === null) return "null";if (value === undefined) return "undefined";// 处理其他类型const type = typeof value;if (type !== "object") return type; // 原始类型直接返回// 对象类型细化return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}// 测试
detectType([]); // "array"
detectType(new Map()); // "map"
detectType(Symbol()); // "symbol"
detectType(42); // "number"

总结对比

方法优点缺点
typeof简单快速,适合原始类型无法区分数组/对象/null
instanceof检测自定义对象不适用原始类型,多窗口环境失效
Object.prototype.toString精准识别所有类型(推荐)语法稍复杂
Array.isArray()数组检测最优解仅适用于数组

最佳实践:

  • 基础类型检测 → typeof
  • 数组检测 → Array.isArray()
  • 完整类型检测 → Object.prototype.toString.call()
  • null 检测 → value === null
http://www.dtcms.com/wzjs/232044.html

相关文章:

  • 北京贸易公司网站制作飓风seo刷排名软件
  • 哪些网站做微课赚钱小网站关键词搜什么
  • 上海学做网站seo推广教程视频
  • 广安网站建设免费b站推广网站有哪些
  • 更换网站后台免费推广链接
  • 深圳网站建设 卓营销推广外包
  • wordpress登录用户错乱北京中文seo
  • 邯郸网站开发公司如何做seo搜索引擎优化
  • 建设展示型网站公司哪家好seo是什么意思如何实现
  • 给朋友做网站警察开找技能培训有哪些
  • python做网站赚钱seo招聘要求
  • 企业网站功能网站宣传
  • 做电商怎么建网站怎么在百度上推广自己的店铺
  • 网站建设中心怎么做太原网络营销公司
  • 网站适配移动端和PC端十大网络舆情案例
  • js建设网站外网百度推广客服中心
  • 甘肃网站定制开发搜索引擎优化的基本手段
  • gofair做网站唐山百度seo公司
  • 中山专业做网站公司搜索引擎平台排名
  • 网站页面设计策划书音乐接单推广app平台
  • 网站建设 笔记最新军事动态最新消息
  • 邯郸网站建设品牌公司足球世界排名前十
  • php和java做网站有哪些平台可以做推广
  • jsp网站建设seo基础培训教程
  • 广州网站建设专家导航网站怎么推广
  • 洛阳网站建设招聘信息自助发稿
  • 昆明网站建设论坛整站优化cms
  • 专做毕业设计的网站必应站长平台
  • 怎么做购物优惠券网站淘宝运营培训
  • 关于成立政府网站建设营业推广的方式有哪些