Jquery之isPlainObject源码分析

今天对Jquery中 isPlainObject 源码分析。

  1.  isPlainObject 方法的作用: 

  用来判断传入参数,是否是对象。
 

  1. 源码分析:isPlainObject: function( obj ) {
    
   // 1.  jQuery.type( obj ):  通过 juery 封装的类型判断方法,如果不是 Object类型,返回false 
 // 2. obj.nodeType :  用来判断是否是dom 节点, 如果是,返回 false 
   // 3.  jQuery.isWindow( obj ) : 这个方法,很多人可能会误解其意思。 其真正意思是:判断指定参数是否是一个窗口,"窗口"就是浏览器内置的Window对象。 如果是,返回false. 
  // 4.  总的来说,这个if判断的意思就是, 如果 传进来的是 不是 object对象 或者 dom节点 或者 “窗口” 就返回 false; 
 if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
 return false; 
 } 
 
 
 try { 
 // 此if判断法: 主要是 防止把 “window.location” 当成 Obect 对象。 
 // 1. obj.constructor : 判断 入参是否 具有 构造函数。 
 // 2. core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) : 判断传入obj 的原型链上是否有 ”isPrototypeOf“ 方法。 如果有则是 object 对象,返回 ture;  注意:只有 Object 对象的原型链上才有 isPrototypeOf 这个方法。
 if ( obj.constructor && 
 !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) { 
 return false; 
 } 
 } catch ( e ) { 
 return false; 
 } 
 
 // 返回 ture; 
 return true; 
},

转载于猿2048:➜《Jquery之isPlainObject源码分析》

评论

暂无评论