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

分析$.isPlainObject

阅读更多
作者:zccst

本次学习$.isPlainObject,是不是一个普通对象。测试对象是否是纯粹的对象(通过 "{}" 或者 "new Object" 创建的)

1,使用场景:

var o = {};

console.log($.isPlainObject(o));//如果是空对象就返回TRUE,否则返回FALSE。



2,下面看一下源码实现:
isPlainObject: function( obj ) {
        // Not plain objects:以下三种情况不是普通对象(1)使用typeof判断;(2)DOM节点(3)window
        // - Any object or value whose internal [[Class]] property is not "[object Object]"
        // - DOM nodes
        // - window
        if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
            return false;
        }

        // Support: Firefox <20
        // The try/catch suppresses exceptions thrown when attempting to access
        // the "constructor" property of certain host objects, ie. |window.location|
        // https://bugzilla.mozilla.org/show_bug.cgi?id=814622
        try {
       //obj的构造函数为真,且obj构造函数的原型对象里没有isPrototypeOf属性,则也不是纯粹的对象
            if ( obj.constructor && !core_hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
                return false;
            }
        } catch ( e ) {
            return false;
        }

        // If the function hasn't returned already, we're confident that
        // |obj| is a plain object, created by {} or constructed with new Object
        return true;
    }



判断过程:
var class2type = {};
var core_hasOwn = class2type.hasOwnProperty;
// Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) {
    class2type[ "[object " + name + "]" ] = name.toLowerCase();
});

core_hasOwn.call( o.constructor.prototype, "isPrototypeOf" )与
o.constructor.prototype.hasOwnProperty( "isPrototypeOf" )是相同的

hasOwnProperty语法:obj.hasOwnProperty(prop) //返回值就是TRUE或FALSE


如果您觉得本文的内容对您的学习有所帮助,您可以微信:
分享到:
评论

相关推荐

    jQuery.isPlainObject-v1.10.2源码

    jQuery.isPlainObject-v1.10.2源码

    jQuery 1.5 API 中文版

    $.blur,.mousedown,.change,.mouseenter,.click,.mouseleave,.dblclick,.mousemove,.error,.mouseout,.focus,.mouseover,.focusin,.mouseup,.focusout,.resize,.keydown,.scroll,.keypress,.select,.keyup,.submit,....

    jQuery1.4 API

    jQuery 1.4.1 速查表 -- Shawphy, 原作:G....get(index) index(subject) 数据缓存 data...$.isEmptyObject(obj) $.isPlainObject(obj) 字符串操作 $.trim(str) URL $.param(obj, [traditional]) 插件编写 $.error(message)

    JQuery权威指南源代码

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

    jquery1.11.0手册

    $.data(ele,[key],[val])1.8- 队列控制 queue(e,[q]) dequeue([queueName]) clearQueue([queueName]) 插件机制 jQuery.fn.extend(object) jQuery.extend(object) 多库共存 jQuery.noConflict([ex]) 属性 ...

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

    说明 jQuery中的isPlainObject() 函数用于判断指定参数是否是一个纯粹的对象,返回值为Boolean类型。...$.isPlainObject( object ) 参数说明: object:任意类型 需要进行判断的任意值。 $.isPlainObject({}); //

    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

    JQuery each打印JS对象的方法

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

    jQuery权威指南-源代码

    6.3.2 $.ajaxSetup()设置全局Ajax /181 6.4 Ajax中的全局事件/184 6.4.1 Ajax全局事件的基本概念/184 6.4.2 ajaxStart与ajaxStop全局事件/184 6.5 综合案例分析—用Ajax实现新闻点评即时更新/187 6.5.1 需求...

    jQuery 1.6.3正式版发布

    #10076:$.inArray crashes IE6 and Chrome if second argument is `null` or `undefined` CSS #6652:Remove filter:alpha(opacity=100) after animation #9572:Support -ms-transform in .css() method #10021:...

    超越Jquery_01_isPlainObject分析与重构

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

    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. ...

    is-what:JS类型检查(受TypeScript支持)功能,如`isPlainObject()isArray()`等。简单而小型的集成

    是什么? :hear-no-evil_monkey: 非常简单的小型JS类型检查功能。 它完全支持TypeScript!... // import functions you want to use like so:import { isString , isDate , isPlainObject } from 'is-what' 首先

    weapp.qrcode.js

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

Global site tag (gtag.js) - Google Analytics