• jQuery1.6 类型判断


    首先定义了

    全局变量class2type = {};

    rdigit = /\d/,//正则判断是否是数字

    toString = Object.prototype.toString;

    通过jQuery.each,定义class2type的属性和值:

      jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {

    class2type[ "[object " + name + "]" ] = name.toLowerCase();

    });

      type: function( obj ) {

    return obj == null ?String( obj ) :class2type[ toString.call(obj) ] || "object";//js 里一切都是对象,所以可以用toString.call(obj)返回对应对象类型

          //如果直接使用这个方法来判断类型的话,IE下document.getElementById认为是object,其他浏览器则认为是function

          //如果传入的值是NaN的话,会返回"number"

      }

      isFunction: function( obj ) {return jQuery.type(obj) === "function";}//在IE下,如果传入document.getElementById,返回的是false,IE为它是object

      isArray: Array.isArray || function( obj ) {return jQuery.type(obj) === "array";}//如果原生Array拥有isArray方法则使用Array.isArray,否则自定义这个方法

      isWindow: function( obj ) {return obj && typeof obj === "object" && "setInterval" in obj;}//判断是否为window对象

      isNaN: function( obj ) {return obj == null || !rdigit.test( obj ) || isNaN( obj );}//

      isEmptyObject: function( obj ) {

    for ( var name in obj ) {//判断的依据是看对象是否有属性或者方法

    return false;

    }

    return true;

    }

  • 相关阅读:
    Web存储
    JavaScript模块化
    jQuery挖源码——事件绑定
    观察者模式——JavaScript
    Node.js之网络小爬虫
    ECMAScript的严格模式
    JavaScript和jQuery的事件
    认识Ajax
    Redis之intset数据结构
    Redis之Hash数据结构
  • 原文地址:https://www.cnblogs.com/tellme/p/2160923.html
Copyright © 2020-2023  润新知