JQ的各种检测

<script>
    var getProto = Object.getPrototypeOf;
    var class2type = {};
    var toString = class2type.toString;//Object.prototype.toString
    var hasOwn = class2type.hasOwnProperty;//Object.prototype.hasOwnProperty
    var fnToString = hasOwn.toString;//Function.prototype.toString
    var ObjectFunctionString = fnToString.call(Object);// Object这个构造函数变成字符串 Object.toSring() =>  "function Object() { [native code] } "

    // 检测是否是一个函数
    var isFunction = function isFunction(obj) {

        // Support: Chrome <=57, Firefox <=52
        // In some browsers, typeof returns "function" for HTML <object> elements
        // (i.e., `typeof document.createElement( "object" ) === "function"`).//创建一个<object></object>标签
        // We don't want to classify *any* DOM node as a function.
        // Support: QtWeb <=3.8.5, WebKit <=534.34, wkhtmltopdf tool <=0.12.5
        // Plus for old WebKit, typeof returns "function" for HTML collections
        // (e.g., `typeof document.getElementsByTagName("div") === "function"`). (gh-4756)
        return typeof obj === "function" && typeof obj.nodeType !== "number" &&
            typeof obj.item !== "function";
        typeof obj.item !== "function";//obj.item这一项不是function

    };

    // 检测是否是一个window对象
    var isWindow = function isWindow(obj) {
        // window.window===window
        return obj != null && obj === obj.window;
    };

    // 标准的检测数据类型的方法
    /* 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 + "";
    return typeof obj === "object" || typeof obj === "function" ?
        class2type[toString.call(obj)] || "object" :
        typeof obj;
}; */
    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;
    };


    // 检查是否为数组或者类数组
    function isArrayLike(obj) {
        var length = !!obj && "length" in obj && obj.length,//!!obj存不存在   最后存的是false 和 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;//判断类数组 &&优先级最大
    }

    // 检测是不是一个纯粹对象「直属类是Object||Object.create(null)」
    var isPlainObject = function isPlainObject(obj) {
        var proto, Ctor;
        if (!obj || toString.call(obj) !== "[object Object]") return false;
        proto = getProto(obj);
        // Objects with no prototype (e.g., `Object.create( null )`) are plain
        if (!proto) return true;
        // Objects with prototype are plain iff they were constructed by a global Object function
        Ctor = hasOwn.call(proto, "constructor") && proto.constructor;
        return typeof Ctor === "function" && fnToString.call(Ctor) === ObjectFunctionString;
    }
    // proto.hasOwn.constructor 判断有没有这个属性  fnToString.call(Ctor)如果这两个相等fnToString.call(Object)  就是纯对象

    // 检测是空对象
    var isEmptyObject = function isEmptyObject(obj) {
        var keys = Object.keys(obj);
        if (typeof Symbol !== "undefined") keys = keys.concat(Object.getOwnPropertySymbols(obj));
        return keys.length === 0;
    };
    // Object.prototype.AA="AA" //是空对象
    // {}

    // 检测是不是一个数字
    var isNumeric = function isNumeric(obj) {
        var type = toType(obj);
        return (type === "number" || type === "string") && !isNaN(obj);}

</script>
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享