Analysis jQuery source (3) - determines whether the incoming object or array function

var class2type = {}, 
     core_toString = class2type.toString;

jQuery.extend({
  isFunction: function(obj) {
    return jQuery.type(obj) === 'function';
  }
  isArray: Array.isArray || function(obj) {
    return jQuery.type(obj) === 'array';
  }

  type: function(obj){
    if (obj == null) {
      return String(obj);
    }
    return typeof obj === 'object' || typeof obj === 'function'  ?
              class2type[core_toString.call(obj)]  ||  'object'  :  typeof obj;
  }
});
 
typeof not distinguish Array, RegExp object type, etc., for the ability typeof jQuery extension, add $ .Type;
For a particular object (e.g., null, Array, RegExp) also precise type judgment;
The use of hook mechanism, the former determine the type, the type common in stores hash table class2type.
jQuery.each('Boolean Number String Function Array Date RegExp Object Error'.split(' '), function(i, name){
    class2type['[object ' + name + ']'] = name.toLowerCase();
});

 

Guess you like

Origin www.cnblogs.com/easonw/p/11505304.html