To prepare

var getProto = Object.getPrototypeOf; Var class2Type = {}; var class2type = {}; var toString = class2type.toString; //Object.prototype.toString var hasOwn = class2type.hasOwnProperty; //Object.prototype.hasOwnProperty var fnToString = hasOwn.toString; / / Function. ToString = > Function. The prototype. The toString var ObjectFunctionString = fnToString. Call (Object); / / = > Function. The prototype. ToString implementation, the method of this change to the Object [of] = > Object. The toString () = > "Function Object () {} [native code]"Copy the code

Check if it is a function

var isFunction = function isFunction(obj) { return typeof obj === "function" && typeof obj.nodeType ! == "number" && typeof obj.item ! == "function"; };Copy the code

Check if it is a Window object

Var isWindow = function isWindow(obj) {// window.window===window = null && obj === obj.window; };Copy the code

Standard way to detect data types

var arr = ["Boolean", "Number", "String", "Function", "Array", "Date", "RegExp", "Object", "Error", "Symbol"]; arr.forEach(function (name) { class2type["[object " + name + "]"] = name.toLowerCase(); }); var toType = function toType(obj) { if (obj == null) return obj + ""; / / null, and undefined directly spliced into string return typeof obj = = = "object" | | typeof obj = = = "function"? class2type[toString.call(obj)] || "object" : typeof obj; };Copy the code

The regular optimization

var toType = function toType(obj) {
    if (obj == null) return obj + "";
    var reg = /^\[object ([a-zA-Z0-9]+)\]$/i;
    return typeof obj === "object" || typeof obj === "function" ?
        reg.exec(toString.call(obj))[1].toLowerCase() :
        typeof obj;
};
Copy the code

Checks whether it is an array or a class array

var isArrayLike = function isArrayLike(obj) { var length = !! obj && "length" in obj && obj.length, type = toType(obj); if (isFunction(obj) || isWindow(obj)) return false; return type === "array" || length === 0 || typeof length === "number" && length > 0 && (length - 1) in obj; };Copy the code

Check for pure Object “class is directly under the Object | | Object. The create (null)”

var isPlainObject = function isPlainObject(obj) { var proto, Ctor; if (! obj || toString.call(obj) ! == "[object Object]") return false; proto = getProto(obj); if (! proto) return true; Ctor = hasOwn.call(proto, "constructor") && proto.constructor; return typeof Ctor === "function" && fnToString.call(Ctor) === ObjectFunctionString; };Copy the code

Check whether an object is empty

var isEmptyObject = function isEmptyObject(obj) { var keys = Object.keys(obj); if (typeof Symbol ! == "undefined") keys = keys.concat(Object.getOwnPropertySymbols(obj)); return keys.length === 0; };Copy the code

Check if it is a number

var isNumeric = function isNumeric(obj) { var type = toType(obj); return (type === "number" || type === "string") && ! isNaN(obj); };Copy the code