JavaScript中常用的四种数据类型检测方式

在日常开发中,经常使用
typeof 判断某个变量或者表达式,属于哪种基本数据类型,
instanceof 判断某种实例,属于哪种引用类型,
Object.prototype.toString.call() 判断某个对象,属于某种内置类型,
而近期接触到了constructor 知道了它也是一种常用的数据类型检测方式(原谅我知识面不宽广[手动飙泪]),出于好奇…
So,总结一下它们四个在项目中的一些实战使用情况。

开始前,我们首先回顾下存在于JavaScript中的9种数据类型

基本数据类型:Number,Boolean,String,null,undefined,Symbol
引用数据类型:Object,Array,function

首先定义一些变量

let num = 1024;
let flag = true;
let sName = 'Zita';
let age = undefined;
let s = Symbol();
let foo = function(){
     };
let timer = null;
let date = new Date();
let regPhone = /^1[3456789]\d{9}$/;
let flowerNames = ['rose', 'carnation', 'babysbreath'];
let flowerInfo = {
     
 	name: 'babysbreath',
 	price: 300,
 	language: 'Silent guard',
};
let flowers = [
	{
     
	   name: 'rose',
	   price: 100,
	},
	{
     
	   name: 'carnation',
	   price: 200,
	},
	{
     
	   name: 'babysbreath',
	   price: 300,
	},
];

然后开始了我们的实战之旅~~~

typeof

typeof num; // number
typeof flag; // boolean
typeof sName; // string
typeof age; // undefined
typeof s; // symbol
typeof foo; // function 
typeof date; // object
typeof regPhone; // object
typeof timer; // object
typeof flowerNames; // object
typeof flowerInfo; // object
typeof flowers; // object

instanceof

date instanceof Date; // true
flowerNames instanceof Array; // true
regPhone instanceof Object; // true
flowerInfo instanceof Object; // true
flowers instanceof Array; // true
foo instanceof Function; // true

其中,Object包含Date、Array,在日常开发工作中我们也常常使用instanceof判断某值是不是数组。
除此之外,我们也可以使用它来判断一个实例是否属于它的父类型

function Aoo(){
     };
function Boo(){
     };
Boo.prototype = new Aoo();
let obj = new Boo();
obj instanceof Boo // true
obj instanceof Aoo // true

constructor

num.constructor == Number; // true
flag.constructor == Boolean; // true
sName.constructor == String; // true
flowerNames.constructor == Array; // true
flowerInfo.constructor == Object; // true
flowers.constructor == Array; // true
foo.constructor == Function; // true

其余定义的变量没在上述代码中书写的,均返回false。
除此之外,它的另一个属性功能是,主要返回对创建此对象的数组函数的引用。

function employee(name,job,born) {
     
	this.name=name;
	this.job=job;
	this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
console.log(bill.constructor);  
// 返回的结果是:
// function employee(name, job, born)
// {this.name = name; this.job = job; this.born = born;}

Object.prototype.toString.call()

Object.prototype.toString.call(num); // [object Number]
Object.prototype.toString.call(flag); // [object Boolean]
Object.prototype.toString.call(sName); //[object String]
Object.prototype.toString.call(timer); // [object Null]
Object.prototype.toString.call(age); // [object Undefined]
Object.prototype.toString.call(s); // [object Symbol]
Object.prototype.toString.call(date); // [object Date]
Object.prototype.toString.call(regPhone); // [object RegExp]
Object.prototype.toString.call(flowerNames); // [object Array]
Object.prototype.toString.call(flowerInfo); // [object Object]
Object.prototype.toString.call(flowers); // [object Array]
Object.prototype.toString.call(foo); // [object Function]

至此,就是这四种方法返回的结果了,至于在项目中你使用哪个更方便合适一些,可以根据我在文章开头各个简述的功能那样去选择,也可以根据项目情况具体情况具体分析哦~~

你可能感兴趣的:(JavaScript,javascript,js)