javaScript中一些常见的数据类型检查校验
作者:程序猿布欧 发布时间:2023-07-02 05:19:09
前言
在JavaScript中,数据类型分为两大类,一种是基础数据类型,另一种则是复杂数据类型,又叫引用数据类型
基础数据类型:数字Number 字符串String 布尔Boolean Null Undefined Symbols BigInt
引用数据类型:日期Dete,对象Object,数组Array,方法Function, 正则regex,带键的集合:Maps, Sets, WeakMaps, WeakSets
基础数据类型和引用数据类型的区别,在之前深拷贝的文章中提到过,这里不做详细赘述。
传送门:javaScript中深拷贝和浅拷贝简单梳理
常见的几种数据校验方式
接下来会针对下面几种数据类型,进行校验
// 基本数据类型
let str = "abc";
let num = 123;
let boo = true;
let undef = undefined;
let testNull = null;
let symb = Symbol("user");
let bigInt = BigInt(9007199254740999);
// 复杂-引用数据类型
let arr = [1, 2, 3, 4];
let func = function () {};
let obj = {};
let date1 = new Date();
let setObj1 = new Set();
let setObj2 = new Set([1, 2, 3]);
let mapObj = new Map();
typeof操作符
typeof操作符,会返回一个字符串,表示未经计算的操作数的类型
/**
* typeof 操作符
*
* 返回一个字符串,表示未经计算的操作数的类型。
*
* */
console.log(typeof str); // string
console.log(typeof num); // number
console.log(typeof boo); // boolean
console.log(typeof undef); // undefined
console.log(typeof testNull); // object
console.log(typeof symb); // symbol
console.log(typeof bigInt); // bigint
console.log(typeof Object(bigInt)); // object
console.log(typeof arr); // object
console.log(typeof func); // function
console.log(typeof obj); // object
console.log(typeof date1); // object
console.log(typeof setObj1); // object
console.log(typeof setObj2); // object
console.log(typeof mapObj); // object
小结
使用typeof操作符的时候,我们可以看到一些较为特殊的情况:
null,数组array,set,map 返回的是对象object
instanceof
instanceof用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
/**
*
* instanceof
*
* 用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
*
* */
console.log(str instanceof String); // false
console.log(new String("abc") instanceof String); // true
console.log(num instanceof Number); // false
console.log(new Number(123) instanceof Number); // true
console.log(boo instanceof Boolean); // false
console.log(new Boolean(true) instanceof Boolean); // false
console.log(undef instanceof undefined);
// Uncaught TypeError: Right-hand side of 'instanceof' is not an object
console.log(testNull instanceof null);
// Uncaught TypeError: Right-hand side of 'instanceof' is not an object
console.log(symb instanceof Symbol); // false
// Symbol不是构造函数,没有new操作符
console.log(bigInt instanceof BigInt); // false
console.log(Object(BigInt("22")) instanceof Object); // true
console.log(Object(BigInt("22")) instanceof BigInt); // true
console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // true
console.log(func instanceof Function); // true
console.log(func instanceof Object); // true
console.log(obj instanceof Object); // true
console.log(obj instanceof Function); // false
console.log(null instanceof Object); // false
console.log(date1 instanceof Object); // true
console.log(setObj1 instanceof Object); // true
console.log(setObj2 instanceof Object); // true
console.log(mapObj instanceof Object); // true
console.log(setObj1 instanceof Array); // false
console.log(setObj2 instanceof Array); // false
console.log(mapObj instanceof Array); // false
constructor
/**
* constructor
*
* 返回创建实例对象的 构造函数的引用。
*
* 注意,此属性的值是对函数本身的引用,而不是一个包含函数名称的字符串
*
* 构造函数.prototype.constructor()
*
* */
// 基本数据类型
let str = "abc";
let num = 123;
let boo = true;
let undef = undefined;
let testNull = null;
let symb = Symbol("user");
let bigInt = BigInt(9007199254740999);
// 复杂-引用数据类型
let arr = [1, 2, 3, 4];
let func = function () {};
let obj = {};
let date1 = new Date();
function constructorFn() {
this.name = "11";
}
let con1 = new constructorFn();
let setObj1 = new Set();
let setObj2 = new Set([1, 2, 3]);
let mapObj = new Map();
console.log(str.constructor); // String
console.log(num.constructor); // Number
console.log(boo.constructor); // Boolean
// console.log(testUndefined.constructor); // Cannot read property 'constructor' of undefined
// console.log(testNull.constructor); // Cannot read property 'constructor' of null
console.log(symb.constructor); // Symbol
console.log(bigInt.constructor); // BigInt
console.log(arr.constructor); // Array
console.log(func.constructor); // Function
console.log(obj.constructor); // Object
console.log(date1.constructor); // Date
console.log(constructorFn.constructor); // Function
console.log(con1.constructor); // constructorFn
console.log(setObj1.constructor); // Set
console.log(setObj2.constructor); // Set
console.log(mapObj.constructor); // Map
/**
*
* 构造函数校验
*
* */
console.log(Function.constructor); // Function
console.log(Object.constructor); // Function
console.log(Array.constructor); // Function
console.log(Date.constructor); // Function
Object.prototype.toString.call && Object.prototype.toString.apply
Object.prototype.toString()
在使用Object.prototype.toString.call或者Object.prototype.toString.apply检查数据类型之前,我们先了解一下Object.prototype.toString和JavaScript中的构造函数Function的原型方法apply和call:
/**
* 返回一个表示该对象的字符串
*
* Object.prototype.toString()
*
* 每个对象都有一个 toString() 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。
* 默认情况下,toString() 方法被每个 Object 对象继承。
*
* 如果此方法在自定义对象中未被覆盖,toString() 返回 "[object type]",其中 type 是对象的类型。以下代码说明了这一点:
*
* */
let isObj = { name: "zhangsan" };
let isBoolean = true;
let isNumber = new Number(123);
let isString = "abc";
let isFun = new Function();
console.log(isObj.toString()); // [object Object]
console.log(isBoolean.toString()); // true
console.log(isNumber.toString()); // 123
console.log(isString.toString()); // abc
console.log(new Date().toString()); // Thu Apr 28 2022 16:37:19 GMT+0800 (中国标准时间)
console.log(isFun.toString()); // function anonymous() {}
call && apply
/**
*
* call() 使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数,function.call(thisArg, arg1, arg2, ...)
*
* apply() 使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数,unc.apply(thisArg, [argsArray])
*
* */
// call基本使用;
function a() {
console.log(this);
}
function b() {
console.log(this);
}
a.call(b); //function b() {}
b.call(a); //function a() {}
call和apply最简单的例子表明了,改变了当前方法的this指向
同时这两个方法的区别在于传参的方式
Object.prototype.toString结合Function.prototype.call && apply
/**
*
* 使用 toString() 检测对象类型可以通过 toString() 来获取每个对象的类型。
* 为了每个对象都能通过 Object.prototype.toString() 来检测,
* 需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为 thisArg。
*
* 那么 Object.prototype.toString 相当于 原生构造函数的实例化对象isNumber,传参数给Object.prototype.toString执行
* 实际上相当于 toString.call(new ***);
*
* */
let str = "abc";
let num = 123;
let boo = true;
let undef = undefined;
let testNull = null;
let symb = Symbol("user");
let bigInt = BigInt(9007199254740999);
// 复杂-引用数据类型
let arr = [1, 2, 3, 4];
let func = function () {};
let obj = {};
let date1 = new Date();
function testFun() {}
let newTest = new testFun();
let newFun = new Function();
let setObj1 = new Set();
let setObj2 = new Set([1, 2, 3]);
let mapObj = new Map();
console.log(Object.prototype.toString.apply(new String("sss"))); // [object String]
console.log(Object.prototype.toString.apply(str)); // [object String]
console.log(Object.prototype.toString.call(num)); // [object Number]
console.log(Object.prototype.toString.call(boo)); // [object Boolean]
console.log(Object.prototype.toString.call(undef)); // [object Undefined]
console.log(Object.prototype.toString.call(testNull)); // [object Null]
console.log(Object.prototype.toString.call(symb)); // [object Symbol]
console.log(Object.prototype.toString.call(Object(bigInt))); // [object BigInt]
console.log(Object.prototype.toString.call(bigInt)); // [object BigInt]
console.log(Object.prototype.toString.apply(arr)); // [object Array]
console.log(Object.prototype.toString.call(func)); // [object Function]
console.log(Object.prototype.toString.call(obj)); // [object Object]
console.log(Object.prototype.toString.call(date1)); // [object Date]
console.log(Object.prototype.toString.call(testFun)); // [object Function]
console.log(Object.prototype.toString.call(newTest)); // [object Object]
console.log(Object.prototype.toString.call(newFun)); // [object Function]
console.log(Object.prototype.toString.call(setObj1)); // [object Set]
console.log(Object.prototype.toString.call(setObj2)); // [object Set]
console.log(Object.prototype.toString.call(mapObj)); // [object Map]
其他校验数据类型的方法:
判断是否是数组:
console.log(Array.isArray([1, 2])); // true
判断一个对象是否是空对象
// 判断空对象
function isEmptyObj(obj) {
for (name in obj) {
console.log(name);
return false; // 不是空对象
}
return true;
}
console.log(isEmptyObj({})); // true
来源:http://lewyon.xyz/checkType.html


猜你喜欢
- 1.视图的概述 视图其实就是一条查询sql语句,用于显示一个或多个表或其他视图中的相关数据。视图将一个查询的结果作为一个表来使用,因此视图可
- 元组的结构在这一小节当中主要介绍在 python 当中元组的数据结构:typedef struct { PyObj
- 对,你没看错,这是我初学 python 时的灵魂发问。我们总会在class里面看见self,但是感觉他好像也没什么用处,就是放在那里占个位子
- 一年一度的元宵节刚刚过去,由于时间关系,在元宵节当天晚上11点多才完成本文灯笼的绘制。这两天又在忙着别的事情,所以现在才跟大家分享。一、效果
- 下面的示例看看这三个函数的具体的区别,其中var_dump和var_export比较少用,但他们两者又很相似。所以可以看看:<?php
- 一般情况下TextArea区输入的文字数量是没有限制的,但是我们可以通过javascript限制表单的文字字数。如下javascript代码
- 按比例获取样本数据或执行任务By:授客 QQ:1033553122开发环境win 10python 3.6.5需求已知每种分类的样本占比数,
- 今天遇到一个问题,就是用pycharm运行python程序,老是会出现Python.exe已停止的对话框。后来我到处在网上搜原因,网上给出的
- 阅读上一篇:你是真正的用户体验设计者吗? Ⅰwrite2vin 的 原文路宛兮写的简介:本文介绍了: 1.关于用户体验的几种观点; 2.关于
- 废话不多说1.win+R 启动“运行”输入cmd 点确定2.输入 cd /d xxxxxxx回车jupyter notebook回车在这里我
- 大家在使用python的过程中,应该在敲代码的时候经常遇到str内置函数,为了防止大家搞混,本文整理归纳了str内置函数。1字符串查找类:f
- 写在前面的话:Part 1记得刚毕业那时,常幻想着自己是个大艺术家,满怀憧憬的想找一份理想的工作。后来入了行,慢慢的发现自己好像不是这块料;
- 虽然今年名义上已经不再管人了,但也不得不掺和进很多人事,这里想简单说说,即使不能帮助这个行业的从业者规划职业道路,也算是把之前摸过的路小结一
- 用python做一个简单的随机点名程序(不重复点名)这是我来到csdn的第一篇文章,内容如果有瑕疵的地方或者代码可以进一步改善,请大家对我指
- 使用索引优化索引是数据库优化最常用也是最重要的手段之一,通过索引通常可以帮助用户解决大多数的MySQL的性能优化问题。数据准备use wor
- 本文实例讲述了Python爬虫框架Scrapy常用命令。分享给大家供大家参考,具体如下:在Scrapy中,工具命令分为两种,一种为全局命令,
- 一、identity的基本用法1.含义identity表示该字段的值会自动更新,不需要我们维护,通常情况下我们不可以直接给identity修
- 在使用Jupyter notebook时有这么一句代码start_frame = imread(“OwnCollection\vehicle
- ZeroClipboard.js是一个支持复制和粘贴的JavaScript插件,目前官方已经到2.x的版本了,但不支持IE9以下的浏览器,而
- 默认情况下,PyCharm中如果有无法错误或者不符合PEP8规范代码下面会有波浪线,语法错误波浪线为红色(如下图的第10行),不符合PEP8