zoukankan      html  css  js  c++  java
  • 分析$.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

  • 相关阅读:
    Mysql主从复制、半同步复制、并行复制
    LVS实现(VS/DR)负载均衡和Keepalived高可用
    Linux系统的高级网络配置(bond、team、网桥)
    Linux系统的日志管理、时间同步、延迟命令at
    Linux系统的内核编译
    Linux系统的vsftpd服务配置
    Linux系统下的软件管理(rpm)、搭建第三方软件库、yum的黑名单
    【笨嘴拙舌WINDOWS】伟大的变革
    程序猿,写的是什么?
    Android-Thread线程的状态
  • 原文地址:https://www.cnblogs.com/zccst/p/3725523.html
Copyright © 2011-2022 走看看