`
zhangyaochun
  • 浏览: 2565949 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

jQuery源码浅谈系列---$.isPlainObject

阅读更多

jQuery.isPlainObject(obj)

 

   ----------测试参数对象是否是纯粹的对象通过“{}”或者“new Object”创建的

 

 

  参数obj--{Object}: 用于测试是否为纯粹的对象。

 

 

 

/*简单的列举几个测试的例子*/
jQuery.isPlainObject({});  //true
jQuery.isPlainObject(1);  //false
jQuery.isPlainObject("zhangyaochun"); //false

 

 

源码选自于1.4.2版本

 

 

var hasOwnProperty  = Object.prototype.hasOwnProperty,
      toString = Object.prototype.toString;
isPlainObject:function(obj){
    //!obj ---一定要是对象
    // toString.call(obj) !== "[object Object]"----因为IE,检测constructor
    //obj.nodeType ----避免不是DOM nodes
    //obj.setInterval ---排除window
   if(!obj || toString.call(obj) !== "[object Object]" ||obj.nodeType ||obj.setInterval){
            return false;
   }
   //是否是new fun()自定义对象
   //constructor是否是继承原型链
   //原型链是否有isPrototypeOf
   if(obj.constructor && !hasOwnProperty.call(obj,"constructor")
       && !hasOwnProperty.call(obj.constructor.prototype,"isPrototypeOf")){
         return false;
   }
   //判断是否有继承关系
   //自己的属性会被首先遍历
    var key;
    for(key in obj){}
    //直接看最后一项是未了加速遍历的过程
    return key === undefined || hasOwnProperty.call(obj,key);
}
0
1
分享到:
评论

相关推荐

    jQuery.isPlainObject-v1.10.2源码

    jQuery.isPlainObject-v1.10.2源码

    jQuery 1.5 API 中文版

    $.jQuery( selector [, context] ), .jQuery( element ), .jQuery( elementArray ), .jQuery( jQueryObject ), .jQuery( ) $.jQuery( html [, ownerDocument] ), .jQuery( html, props ) $.jQuery( fn ) jQuery ...

    jquery1.11.0手册

    jQuery 1.11.0 速查表 核心 jQuery 核心函数 jQuery([sel,[context]]) jQuery(html,[ownerDoc])1.8* jQuery(callback) jQuery.holdReady(hold) jQuery 对象访问 each(callback) size() length selector ...

    jQuery1.4 API

    jQuery 1.4.1 速查表 -- Shawphy, 原作:G. Scott Olson 核心 jQuery 核心函数 jQuery(expr, [context]) jQuery(html, [ownerDoc]) jQuery(html, props) jQuery(elements) jQuery() jQuery(callback) jQuery 对象访问...

    JQuery权威指南源代码

    使用$.isPlainObject()函数检测对象是否为原始对象 使用$.contains()函数检测两个节点是否包含 使用$.param()进行数组元素序列化 使用函数$.extend()扩展工具函数 使用函数$.proxy()改变事件函数的作用域 使用...

    jQuery权威指南-源代码

    书名:jQuery权威指南(系统介绍jQuery方方面面,囊括118个实例和2个综合案例,实战性强) 作者:陶国荣 著 书号:978-7-111-32543-7 定价:59.00元 出版社:机械工业出版社华章公司 出版时间:2011年1月 编辑推荐:...

    jQuery 1.4.1 中文参考

    11.4.5 jQuery.isPlainObject(obj) 193 11.5 字符串操作 194 11.5.1 jQuery.trim(str) 194 11.6 URL 194 11.6.1 jQuery.param(obj, [traditional]) 194 11.7 插件编写 196 11.7.1 jQuery.error(message) 196 12. ...

    详解jQuery中的isPlainObject()使用方法

    jQuery中的isPlainObject() 函数用于判断指定参数是否是一个纯粹的对象,返回值为Boolean类型。 “纯粹的对象”,就是通过 { }、new Object()、Object.create(null) 创建的对象。 这个方法的作用是为了跟其他的 ...

    jQuery 1.6.3正式版发布

    #9897:try-catch isPlainObject detection #10076:$.inArray crashes IE6 and Chrome if second argument is `null` or `undefined` CSS #6652:Remove filter:alpha(opacity=100) after animation #9572:...

    js实现数组去重、判断数组以及对象中的内容是否相同

    代码如下: /* *数组元素去重 */ if(typeof Array....i++){ if($.isPlainObject(this[i]) && $.isPlainObject(this[i+1])){ if(o2o(this[i],this[i+1])){ this.splice(i,1); } }else if($.isArray(this[i]) && $.is

    different-contexts-isPlainObject-test:测试具有不同全局上下文的构造函数instanceof构造函数问题

    不同上下文的isPlainObject测试测试具有不同全局上下文的构造函数instanceof构造函数问题npm installnpm start

    JQuery each打印JS对象的方法

    但是有时候返回的是一个对象,json格式的数据,jQuery可以用这个方法循环遍历读出对象的值,假如这个对象名称是obj,循环遍历打印它的值: 代码如下: $.each(obj,function(key,val){ if($.isPlainObject(val) || $...

    超越Jquery_01_isPlainObject分析与重构

    isPlainObject是Jquery1.4后提供的新方法,用于判断对象是否是纯粹的对象(通过 {} 或者 new Object 创建的)。

    weapp.qrcode.js

    var isPlainObject = function isPlainObject(obj) { if (!obj || toStr.call(obj) !== '[object Object]') { return false; } var hasOwnConstructor = hasOwn.call(obj, 'constructor'); var ...

    is-plain-object:如果给定值是由Object构造函数创建的对象,则返回true

    用法与es模块import { isPlainObject } from 'is-plain-object' ; 或与commonjs const { isPlainObject } = require ( 'is-plain-object' ) ; 由Object构造函数或Object.create(null)创建时为true 。 ...

Global site tag (gtag.js) - Google Analytics