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.局部变量目标为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条)

刀客源码 游客评论