• 分析$.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

  • 相关阅读:
    洛谷 P1037 产生数
    阿里实习储备知识
    腾讯后台开发面试总结(别人的)
    两个栈实现队列的功能
    栈里的元素升序排列
    二叉树路径和
    硬币组合问题
    九月十月百度,迅雷,华为,阿里巴巴笔试面试六十题(第411~470题)
    轻松搞定面试中的二叉树题目
    july教你如何迅速秒杀掉:99%的海量数据处理面试题
  • 原文地址:https://www.cnblogs.com/zccst/p/3725523.html
Copyright © 2020-2023  润新知