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
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/235081.html