42 lines
998 B
JavaScript
42 lines
998 B
JavaScript
|
|
/**
|
|
* 获取指定变量类型名称
|
|
* getDataTypeName(1) == Number
|
|
* getDataTypeName("") == String
|
|
* getDataTypeName(null) == Null
|
|
* getDataTypeName(undefined) == Undefined
|
|
* getDataTypeName(new Date()) == Date
|
|
* getDataTypeName(new Error()) == Error
|
|
*
|
|
* @param {*} v
|
|
* @returns
|
|
*/
|
|
function getDataTypeName(v){
|
|
if (v === null) return 'Null'
|
|
if (v === undefined) return 'Undefined'
|
|
if(typeof(v)==="function") return "Function"
|
|
return v.constructor && v.constructor.name;
|
|
};
|
|
function isPlainObject(obj){
|
|
if (typeof obj !== 'object' || obj === null) return false;
|
|
var proto = Object.getPrototypeOf(obj);
|
|
if (proto === null) return true;
|
|
var baseProto = proto;
|
|
|
|
while (Object.getPrototypeOf(baseProto) !== null) {
|
|
baseProto = Object.getPrototypeOf(baseProto);
|
|
}
|
|
return proto === baseProto;
|
|
}
|
|
function isNumber(value){
|
|
return !isNaN(parseInt(value))
|
|
}
|
|
|
|
module.exports = {
|
|
getDataTypeName,
|
|
isNumber,
|
|
isPlainObject
|
|
}
|
|
|
|
|