zoukankan      html  css  js  c++  java
  • jQuery isPlainObject

    第一次看jQuery的源代码,转得好晕,苦逼的isPlainObject, 判断是否为纯粹的对象

    isPlainObject: function( obj ) {

    if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {//为空,节点,window对象,非object返回false;

    return false;

    }

    if ( obj.constructor &&

    !hasOwn.call(obj, "constructor") &&

    !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {//通过判断isPrototypeOf属性,检测原型链是否在Object.prototype,通过字面量或自定义类(构造器)创建的对象都会继承该属性方法,

    return false;

    }

            var key;

    for ( key in obj ) {}

    return key === undefined || hasOwn.call( obj, key );

    }

    这个方法存在一些问题:

    1.指定constructor

      function a(){this.xx=1;}

      var c=new a;

      c.constructor= Object;//!hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ==false

      isPlainObject(c)//true

      如果a.prototype.cc=function(){};那么会出现两种情况:

        一、如果a没有定义任何方法或者属性,那么结果为所有浏览器都为为false;

        二、如果a定义了,如this.xx=1;那么IE会返回true,其他浏览器均返回false;

      出现这种情况在于var key;for ( key in obj ) {};列举obj属性的时候,IE和其他浏览器不同,IE会把扩展的方法先列举出来,而其他浏览器相反

    2.Object.prototype.xx=1;

      c={};

      isPlainObject(c)//false

      这种情况是因为扩展了Object.prototype,但是IE下,如果c定义了某个属性或者方法c={"a":1},会返回true

    3.document.getElementById;

      因为IE下会认为是object,所以isPlainObjcet(document.getElementById)会返回true,其他浏览器认为是function,返回false

    4.location, history, screen

  • 相关阅读:
    Ubuntu中开启Telnet服务
    单片机串口通讯RXD与TXD如何对接详解
    KEIL, a Smart Comliler
    linux mail 命令(转载)
    VC程序在没装VC的机器中运行
    srand()以及rand()函数用法
    VC菜菜鸟:基于CFree的HelloWorld程序
    Keil使用中的若干问题(转)
    VMWARE 重新安装出错
    Linux个人学习笔记(编辑中)
  • 原文地址:https://www.cnblogs.com/tellme/p/2159741.html
Copyright © 2011-2022 走看看