欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js判断数据类型(js中if判断两个条件)

程序员文章站 2023-12-05 21:34:40
1.判断对象类型的方法://万能的类型判断方法,可以判断所有对象的类型const objecttostring = object.prototype.tostring;const totypestri...

1.判断对象类型的方法:

//万能的类型判断方法,可以判断所有对象的类型
const objecttostring = object.prototype.tostring;
const totypestring = (value) => objecttostring.call(value);
//判断是否是array
const isarray = array.isarray;
//判断是否是map
const ismap = (val) => totypestring(val) === '[object map]';
//判断是否是set
const isset = (val) => totypestring(val) === '[object set]';
//判断是否是date
const isdate = (val) => val instanceof date;
//判断是否是function
const isfunction = (val) => typeof val === 'function';
//判断是否是string
const isstring = (val) => typeof val === 'string';
//判断是否是symbol
const issymbol = (val) => typeof val === 'symbol';
//判断是否是非空对象
const isobject = (val) => val !== null && typeof val === 'object';
//判断是否是promise
const ispromise = (val) => {
return isobject(val) && isfunction(val.then) && isfunction(val.catch);
};
//判断是否是普通的object对象
const isplainobject = (val) => totypestring(val) === '[object object]';
//特别注意:
1.typeof 对象判断方法:
typeof null // "object";
typeof undefined //"undefined"
2.声明未赋值的变量的类型为undefined:
let abc //undefined

2.判断对象是否有某个属性的方法:

const hasownproperty = object.prototype.hasownproperty;
const hasown = (val, key) => hasownproperty.call(val, key);

3.javascript的全局变量对象:

infinity,undefined,nan,isfinite,isnan,parsefloat,parseint,decodeuri,
decodeuricomponent,encodeuri,encodeuricomponent,math,number,date,array,
object,boolean,string,regexp,map,set,json,intl