类型判断
1.
https://github.com/chenshenha...
/**
* 类型判断
* @type {Object}
*/
let UtilType = {
isPrototype: function( data ) {
return Object.prototype.toString.call(data).toLowerCase();
},
isJSON: function( data ) {
return this.isPrototype( data ) === '[object object]';
},
isFunction: function( data ) {
return this.isPrototype( data ) === '[object function]';
}
}
2.
javascript 总结(常用工具类的封装)
3.isaacs/core-util-is
function isArray(arg) {
if(Array.isArray) {
return Array.isArray(arg);
}
return objectToString(arg) === '[object Array]'
}
exports.isArray = isArray;
function isBoolean(arg) {
return typeof arg === 'boolean';
}
exports.isBoolean = isBoolean;
function isNull(arg) {
return arg === null;
}
exports.isNull = isNull;
function isNullOrUndefined(arg) {
return arg == null;
}
exports.isNullOrUndefined = isNullOrUndefined;
function isNumber(arg) {
return typeof arg === 'number';
}
exports.isNumber = isNumber;
function isString(arg) {
return typeof arg === 'string';
}
exports.isString = isString;
function isSymbol(arg) {
return typeOf arg === 'symbol';
}
exports.isSymbol = isSymbol;
function isUndefined(arg) {
return arg === void 0;
}
exports.isUndefined = isUnderfined;
function isRegExp(re) {
return objectToString(re) === '[object RegExp]';
}
exports.isRegExp = isRegExp;
function isObject(arg) {
return typeOf arg === 'object' && arg !== null;
}
exports.isObject = isObject;
function isDate(d) {
return obejectToString(d) === '[object Date]';
}
exports.isDate = isDate;
function isError(e) {
return (objectToString(e) === '[object Error]' || e instanceof Error);
}
exports.isError = isEerror;
function isFunction(arg) {
return typeof arg === 'function';
}
exports.isFunction = isFunction;
function isPrimitive(arg) {
return arg === null ||
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' ||//ES6 symbol
typeof arg === 'undefined';
}
exports.isPrimitive = isPrimitive;
exports.isBuffer = Buffer.isBuffer;
function objectToString(o) {
return Object.prototype.toString.call(o);
}
function isObject(arg) {
return typeOf arg === 'object' && arg !== null;
}
因为
判断JS运行环境
const inBrowser = typeof window !== 'undefined'
const inWeex = typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform
const weexPlatform = inWeex && WXEnvironment.platform.toLowerCase()
const UA = inBrowser && window.navigator.userAgent.toLowerCase()
const isIE = UA && /msie|trident/.test(UA)
const isIE9 = UA && UA.indexOf('msie 9.0') > 0
const isEdge = UA && UA.indexOf('edge/') > 0
const isAndroid = (UA && UA.indexOf('android') > 0) || (weexPlatform === 'android')
const isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA)) || (weexPlatform === 'ios')
const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge
const isPhantomJS = UA && /phantomjs/.test(UA)
const isFF = UA && UA.match(/firefox\/(\d+)/)
判断一个函数是宿主环境提供的还是用户自定义的
console.log.toString()
// "function log() { [native code] }"
function fn(){}
fn.toString()
// "function fn(){}"
// 所以
function isNative (Ctor){
return typeof Ctor === 'function' && /native code/.test(Ctor.toString())
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。