建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

24、判断一个值是什么类型有哪些方法?(必会)(js中如何判断数据类型)

GG网络技术分享 2025-03-18 16:14 10


24、判断一个值是什么类型有哪些方法?(必会)

方法

1、typeof 运算符

2、instanceof 运算符

instanceof 严格来说是 Java 中的一个双目运算符,用来测试一个对象是否为一个类的实例,用法为:

3、Object.prototype.toString方法

(1)在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object”,“function”,“symbol”(ES6 新增)七种。

(2)对于数组、null、对象来说,其关系错综复杂,使用typeof 都会统一返回 “object” 字符串。

(3)要想区别对象、数组、函数单纯使用 typeof 是不行的,JavaScript 中,通过Object.prototype.toString 方法,判断某个对象值属于哪种内置类型。

(4)在介绍 Object.prototype.toString 方法之前,我们先把 toString()方法和Object.prototype.toString.call()方法进行对比。

(5)toString()方法和 Object.prototype.toString.call()方法对比。

js中如何判断数据类型

方法一、js内置方法typeof

检测基本数据类型的最佳选择是使用typeof

typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object”,“function”,“symbol” (ES6新增)七种。

对于数组、null、对象来说,其关系错综复杂,使用 typeof 都会统一返回 “object” 字符串。

示例:

var bool = true

var num = 1

var str = \'abc\'

var und = undefined

var nul = null

var arr = [1,2,3]

var obj = {}

var fun = function(){}

var reg = new RegExp()

console.log(typeof bool); //boolean

console.log(typeof num); //number

console.log(typeof str); //string

console.log(typeof und); //undefined

console.log(typeof nul); //object

console.log(typeof arr); //object

console.log(typeof obj); //object

console.log(typeof reg); //object

console.log(typeof fun); //function

由结果可知,除了在检测null时返回 object 和检测function时放回function。对于引用类型返回均为object。

方法二、Object.prototype.toString()

Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型。

var obj = {};

obj.toString() // \"[object Object]\"上面代码调用空对象的toString方法,结果返回一个字符串object Object,其中第二个Object表示该值的构造函数。这是一个十分有用的判断数据类型的方法。

Object.prototype.toString.call(value)

上面代码表示对value这个值调用Object.prototype.toString方法。

不同数据类型的Object.prototype.toString方法返回值如下。

数值:返回[object Number]。

字符串:返回[object String]。

布尔值:返回[object Boolean]。

undefined:返回[object Undefined]。

null:返回[object Null]。

数组:返回[object Array]。

arguments 对象:返回[object Arguments]。

函数:返回[object Function]。

Error 对象:返回[object Error]。

Date 对象:返回[object Date]。

RegExp 对象:返回[object RegExp]。

其他对象:返回[object Object]。

那么利用这个特性,可以写出一个比typeof运算符更准确的类型判断函数。

封装出一个判断类型的函数如下:

var type = function (o){

var s = Object.prototype.toString.call(o);

return s.match(/\\[object (.*?)\\]/)[1].toLowerCase();

};

type({}); // \"object\"

type([]); // \"array\"

type(5); // \"number\"

type(null); // \"null\"

type(); // \"undefined\"

type(/abcd/); // \"regex\"

type(new Date()); // \"date\"

另外:还可以加上专门判断某种类型数据的方法

var type = function (o){

var s = Object.prototype.toString.call(o);

return s.match(/\\[object (.*?)\\]/)[1].toLowerCase();

};

var arr = [\'Null\', \'Undefined\', \'Object\', \'Array\', \'String\', \'Number\',

\'Boolean\', \'Function\', \'RegExp\']

arr.forEach(function (t) {

type[\'is\' + t] = function (o) {

return type(o) === t.toLowerCase();

};

});

之后我们可以通过封装出的方法去在不同需求时使用:如下

type.isObject({}) // true

type.isNumber(NaN) // true

type.isRegExp(/abc/) // true

以上就是js中如何判断数据类型的详细内容,更多请关注网站的网其它相关文章!

js中如何判断数据类型 (https://www.wpmee.com/) javascript教程 第1张

标签:

提交需求或反馈

Demand feedback