js判断数据类型

typeof "";  //string
typeof 1;   //number
typeof false; //boolean
typeof undefined; //undefined
typeof function(){}; //function

typeof {}; //object
typeof null; //object
typeof []; //object
typeof new Date(); //object
typeof new RegExp(); //object

typeof Symbol(); //symbol
{} instanceof Object; //true
[] instanceof Array;  //true
[] instanceof Object; //true

判断规则:沿着new a的_proto_属性这条线来找,同时沿着A的prototype属性这条线,若两条线能找到同一个引用,即同一个对象,则返回true。

在这里插入图片描述

在这里插入图片描述

### JavaScript 判断数据类型的方法 在 JavaScript 中,判断数据类型是一个常见的需求。以下是几种常用的方法及其示例: #### 1. `typeof` 操作符 `typeof` 是最基础的判断数据类型的方式,它可以返回以下字符串:`"number"`、`"boolean"`、`"string"`、`"symbol"`、`"object"`、`"undefined"` 和 `"function"`。需要注意的是,`typeof null` 会返回 `"object"`,这是一个已知的缺陷[^3]。 ```javascript console.log(typeof 42); // "number" console.log(typeof "hello"); // "string" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined" console.log(typeof null); // "object" (错误) console.log(typeof []); // "object" console.log(typeof {}); // "object" console.log(typeof function() {}); // "function" ``` #### 2. `instanceof` 操作符 `instanceof` 用于检测构造函数的 `prototype` 属性是否出现在某个实例对象的原型链上。它通常用于判断复杂数据类型(如数组、日期等)[^1]。 ```javascript console.log([] instanceof Array); // true console.log(new Date() instanceof Date); // true console.log({} instanceof Object); // true console.log(null instanceof Object); // false ``` #### 3. `constructor` 属性 每个对象都有一个 `constructor` 属性,指向创建该对象的构造函数。可以通过检查 `constructor` 来判断数据类型[^1]。 ```javascript console.log(([]).constructor === Array); // true console.log((new Date()).constructor === Date); // true console.log(({}).constructor === Object); // true console.log((42).constructor === Number); // true console.log(("hello").constructor === String); // true ``` #### 4. `Object.prototype.toString` 方法 `toString` 是最可靠的方法之一,因为它可以准确地返回数据类型的字符串表示。通过调用 `Object.prototype.toString.call(value)`,可以获得更精确的结果[^2]。 ```javascript console.log(Object.prototype.toString.call(42)); // "[object Number]" console.log(Object.prototype.toString.call("hello")); // "[object String]" console.log(Object.prototype.toString.call(true)); // "[object Boolean]" console.log(Object.prototype.toString.call(null)); // "[object Null]" console.log(Object.prototype.toString.call(undefined)); // "[object Undefined]" console.log(Object.prototype.toString.call([])); // "[object Array]" console.log(Object.prototype.toString.call({})); // "[object Object]" console.log(Object.prototype.toString.call(new Date())); // "[object Date]" console.log(Object.prototype.toString.call(/abcd/)); // "[object RegExp]" ``` ### 注意事项 - `typeof` 对于基本数据类型(如 `number`、`string` 等)非常有效,但对于复杂数据类型(如数组、日期等)可能不够准确。 - `instanceof` 需要注意跨窗口或跨 iframe 的情况,可能会导致不正确的结果。 - 使用 `Object.prototype.toString` 是一种推荐的方式,因为它能够处理所有内置数据类型,并且不会受到跨窗口问题的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值