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

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

//万能的类型判断方法,可以判断所有对象的类型const objectToString = Object.prototype.toString;const toTypeString = (value) => objectToString.call(value);//判断是否是Arrayconst isArray = Array.isArray;//判断是否是Mapconst isMap = (val) => toTypeString(val) === \'[object Map]\';//判断是否是Setconst isSet = (val) => toTypeString(val) === \'[object Set]\';//判断是否是Dateconst isDate = (val) => val instanceof Date;//判断是否是Functionconst isFunction = (val) => typeof val === \'function\';//判断是否是Stringconst isString = (val) => typeof val === \'string\';//判断是否是Symbolconst isSymbol = (val) => typeof val === \'symbol\';//判断是否是非空对象const isObject = (val) => val !== null && typeof val === \'object\';//判断是否是Promiseconst 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
(0)
小多多的头像小多多创始人

相关推荐

发表回复

登录后才能评论